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 Java representation of popular hash table data structure which 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 get method of HashMap works, In Java, equals() and hashcode() method are an integral part of storing and retrieving object 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 index. Elements can be inserted and deleted using positions and duplicate elements are allowed in the list.
There are multiple implementation of List is available in Java e.g. ArrayList, which is based in array and LinkedList which is based in linked list data structure. Now let's see a Java program which is used to convert a Map to List.
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 copy constructor of Collection itself you can easily convert this Collection of Map values into 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 earlier example, 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.
You can see that how mappings from a Map is converted into a List which is printed in the console.
Here is also a hierarchical diagram of Java Collection framework, you can see Map is not a subclass or subinterface of Collection, they are from the sepearte hierarchy.
That'a all about how to convert Map to List in Java. There are many situation when you need this e.g. passing Map's content into a legacy or library method which accept List. You can use this example to convert a hash map to list using its key value ,by using key we make ArrayList of keys also ArrayList of values . If you like to know more about data structure provided by Java in Collection framework, see top 5 data structures from Java API.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java
Algorithms and Data Structures - Part 1 and 2
Data Structures in Java 9 by Heinz Kabutz
Now let's understand what is a List in Java, It is also an interface to provide an ordered collection based on index. Elements can be inserted and deleted using positions and duplicate elements are allowed in the list.
There are multiple implementation of List is available in Java e.g. ArrayList, which is based in array and LinkedList which is based in linked list data structure. Now let's see a Java program which is used to convert a Map to List.
Map to List in Java
Here is our sample program to convert a Map to List in Java. This example is divided into three parts; In 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 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 copy constructor of Collection itself you can easily convert this Collection of Map values into 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 earlier example, 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.
Java Program to convert a Map to a List
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; /** * Java Program to Convert HashMap into ArrayList in Java */ public class MapToList { public static void main(String... args) { HashMap<String, Integer> schoolAgeCriteria = new HashMap<String, Integer>(); // preparing HashMap with keys and values schoolAgeCriteria.put("NursuryClass age criteria", 3); schoolAgeCriteria.put("KinderGarden1 age criteria ", 4); schoolAgeCriteria.put("KinderGarden2 age criteria ", 5); schoolAgeCriteria.put("PrimarySchool age criteria", 6); System.out.println("Size of schoolAgeCriteria Map: " + schoolAgeCriteria.size()); // 1st Example : Converting HashMap keys into ArrayList Set<String> keySet = schoolAgeCriteria.keySet(); List<String> schoolKeyList = new ArrayList<String>(keySet); System.out.println("Size of Key list from Map: " + schoolKeyList.size()); // print list element System.out.println("Printing HashMap keys from converted list : "); for (String key : schoolKeyList) { System.out.println(key); } // 2nd Example : Converting HashMap Values into ArrayList Collection<Integer> values = schoolAgeCriteria.values(); List<Integer> schoolValueList = new ArrayList<Integer>(values); System.out.println("Size of Value list from Map: " + schoolValueList.size()); // print values from list System.out.println("Printing HashMap values from converted list :"); for (Integer value : schoolValueList) { System.out.println(value); } // 3rd Example : Converting HashMap into ArrayList using Entry Set Set<Entry<String, Integer>> set = schoolAgeCriteria.entrySet(); List<Entry<String, Integer>> schoolAgeCriteriaList = new ArrayList<>(set); Iterator<Entry<String, Integer>> it = schoolAgeCriteriaList.iterator(); while (it.hasNext()) { Entry<String, Integer> entry = it.next(); System.out.println("Entry from converted list : " + entry); } } } 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 is converted into a List which is printed in the console.
Here is also a hierarchical diagram of Java Collection framework, you can see Map is not a subclass or subinterface of Collection, they are from the sepearte hierarchy.
That'a all about how to convert Map to List in Java. There are many situation when you need this e.g. passing Map's content into a legacy or library method which accept List. You can use this example to convert a hash map to list using its key value ,by using key we make ArrayList of keys also ArrayList of values . If you like to know more about data structure provided by Java in Collection framework, see top 5 data structures from Java API.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java
Algorithms and Data Structures - Part 1 and 2
Data Structures in Java 9 by Heinz Kabutz
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.