How to convert a Map to List in Java? HashMap to ArrayList Example (original) (raw)

Before converting a Map to a List in Java, we should be very clear about these data structures which are widely used in Java. So let's begin with Map. What is Map? Map is an Interface in Java which store key and value object. It's a Java representation of a popular hash table data structure that allows you to search an existing element in O(1) time, at the same time also makes insertion and removal easier. We use a key object to retrieve the value object by using hashing functionality provided by Map. As we have seen in how the get method of HashMap works, In Java, the equals() and hashcode() methods are an integral part of storing and retrieving objects from it. The map allows duplicate values but no duplicate keys.

Map has its implementation in various classes like HashMap, ConcurrentHashMap, and TreeMap. The Map interface also provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings.

Now let's understand what is a List in Java, It is also an interface to provide an ordered collection based on an index. Elements can be inserted and deleted using positions and duplicate elements are allowed in the list.

There are multiple implementations of List is available in Java like ArrayList, which is based on an array, and LinkedList which is based on a linked list data structure. Now let's see a Java program that is used to convert a Map to a List.

How to convert a Map to List in Java with Example

Here is our sample program to convert a Map to a List in Java. This example is divided into three parts; In the first part, we have converted keys of HashMap into List. Map allows you to get a view of all keys of Map as Set because duplicate keys are not permitted. If you know how to convert Set to List, you can easily convert this Set of keys into a List of keys.

In 2nd example, we have converted all values of Map into List. Map provides a Collection view of all values because values can be duplicates. By using the copy constructor of Collection itself you can easily convert this Collection of Map values into a List.

In 3rd example, we will see how to convert all entries of HashMap into List. Just like keys, Map also provides a Set view of all entries. Similar to the earlier examples, we can also store all entries into List.

You can also read Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about different collection classes in Java.

Here is a also a nice diagram which provides a quick guide of when to use which Collection class in Java:

Java Collection Cheatsheet

Java Program to convert a Map to a List

Here is our complete Java program to convert a given Map to a List in Java, for example how to convert a HashMap into an ArrayList in Java.

import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map.Entry; import java.util.Set;

/**

}

Output Size of schoolAgeCriteria Map: 4 Size of Key list from Map: 4 Printing HashMap keys from converted list : PrimarySchool age criteria KinderGarden1 age criteria KinderGarden2 age criteria NursuryClass age criteria Size of Value list from Map: 4 Printing HashMap values from converted list : 6 4 5 3 Entry from converted list : PrimarySchool age criteria=6 Entry from converted list : KinderGarden1 age criteria =4 Entry from converted list : KinderGarden2 age criteria =5 Entry from converted list : NursuryClass age criteria=3

You can see that how mappings from a Map are converted into a List which is printed in the console.

Here is also a hierarchical diagram of the Java Collection framework, you can see Map is not a subclass or subinterface of Collection, they are from the separate hierarchy.

Map to List in Java

That's all about how to convert Map to List in Java. There are many situations when you need this like passing Map's content into a legacy or library method which accepts List. You can use this example to convert a hashmap to a list using its key value, by using the key we make ArrayList of keys also ArrayList of values. If you like to know more about the data structure provided by Java in the Collection framework, see the top 5 data structures from Java API.

Other Java Array and Collection tutorials You may like

Thanks for reading this article so far. If you like this Map to List conversion tutorial then, please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

And, now is the quiz time, which one is your favorite way to convert a Map to a List in Java? By using keys, values or entry set?