Convert Map to List in Java example shows how to convert Map to List in Java. The example also shows how to convert HashMap to ArrayList or LinkedList containing values, keys or entries of the Map.
We are going to use the Employee class for this example. Let’s first create a HashMap containing the Employee objects as given below.
1 2 3 4 5 6 |
HashMap<String, Employee> employeeMap = new HashMap<String, Employee>(); employeeMap.put("emp01", new Employee("emp01", "Jason", "IT")); employeeMap.put("emp02", new Employee("emp02", "Aaron", "Supply Chain")); employeeMap.put("emp03", new Employee("emp03", "Oliver", "Marketing")); employeeMap.put("emp04", new Employee("emp04", "Raj", "IT")); |
The HashMap contains Employee id as the keys and Employee objects as the values.
How to convert HashMap keys to List?
To get all the keys of the HashMap, use the keySet
method of HashMap class.
1 |
public Set<K> keySet() |
This method returns a Set view of the keys of the HashMap. You can then create a List from the keys of the HashMap using the constructor of ArrayList or LinkedList which accepts Collection as an argument.
1 |
public ArrayList(Collection <? extends E> collection) |
This constructor returns a List containing all the elements of the collection in the order they are returned from collection’s iterator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.javacodeexamples.collectionexamples; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class ConvertMapToListExample { public static void main(String[] args) { HashMap<String, Employee> employeeMap = new HashMap<String, Employee>(); employeeMap.put("emp01", new Employee("emp01", "Jason", "IT")); employeeMap.put("emp02", new Employee("emp02", "Aaron", "Supply Chain")); employeeMap.put("emp03", new Employee("emp03", "Oliver", "Marketing")); employeeMap.put("emp04", new Employee("emp04", "Raj", "IT")); /* * Convert HashMap to List(ArrayList) containing map keys * using keySet method of HashMap. */ List<String> aListEmployeeIds = new ArrayList<String>(employeeMap.keySet()); System.out.println("ArrayList containing HashMap Keys: "); for(String strEmpId : aListEmployeeIds) System.out.println(strEmpId); } } |
Output
1 2 3 4 5 |
ArrayList containing HashMap Keys: emp01 emp03 emp02 emp04 |
Note:
You may have observed from the output that the keys are not in the order they were inserted. That is because HashMap does not maintain the insertion order of the elements.
How to convert HashMap values to List?
To get all the values stored in the HashMap, use the values
method of the HashMap class.
1 |
public Collection<V> values() |
This method returns Collection view of the values stored in the HashMap. You can then create a List from the values of the HashMap using the constructor of ArrayList or LinkedList which accepts Collection as an argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.javacodeexamples.collectionexamples; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class ConvertMapToListExample { public static void main(String[] args) { HashMap<String, Employee> employeeMap = new HashMap<String, Employee>(); employeeMap.put("emp01", new Employee("emp01", "Jason", "IT")); employeeMap.put("emp02", new Employee("emp02", "Aaron", "Supply Chain")); employeeMap.put("emp03", new Employee("emp03", "Oliver", "Marketing")); employeeMap.put("emp04", new Employee("emp04", "Raj", "IT")); /* * Convert HashMap to List(ArrayList) containing map values * using values method of HashMap. */ List<Employee> aListEmployees = new ArrayList<Employee>(employeeMap.values()); System.out.println("ArrayList containing HashMap Values: "); for(Employee empObj : aListEmployees) System.out.println(empObj); } } |
Output
1 2 3 4 5 |
ArrayList containing HashMap Values: [emp01 : Jason : IT] [emp03 : Oliver : Marketing] [emp02 : Aaron : Supply Chain] [emp04 : Raj : IT] |
Note: You may have observed from the output that the values are not in the order they were inserted. That is because HashMap does not maintain the insertion order of the elements.
How to convert HashMap entries to List?
To get all the entries stored in the HashMap, use entrySet
method of HashMap.
1 |
public Set<Map.Entry<K,V>> entrySet() |
This method returns Set view of the mappings (entries) stored in the HashMap. You can then create a List from the Set containing HashMap entries using the constructor of ArrayList or LinkedList which accepts Collection as an argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.javacodeexamples.collectionexamples; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ConvertMapToListExample { public static void main(String[] args) { HashMap<String, Employee> employeeMap = new HashMap<String, Employee>(); employeeMap.put("emp01", new Employee("emp01", "Jason", "IT")); employeeMap.put("emp02", new Employee("emp02", "Aaron", "Supply Chain")); employeeMap.put("emp03", new Employee("emp03", "Oliver", "Marketing")); employeeMap.put("emp04", new Employee("emp04", "Raj", "IT")); /* * Convert HashMap to List(ArrayList) containing map entries * using entrySet method of HashMap. */ List<Map.Entry<String, Employee>> aListEntries = new ArrayList<Map.Entry<String, Employee>>(employeeMap.entrySet()); System.out.println("ArrayList containing HashMap Entries: "); for(Map.Entry<String, Employee> empEntry : aListEntries) System.out.println(empEntry); } } |
Output
1 2 3 4 5 |
ArrayList containing HashMap Entries: emp01=[emp01 : Jason : IT] emp03=[emp03 : Oliver : Marketing] emp02=[emp02 : Aaron : Supply Chain] emp04=[emp04 : Raj : IT] |
This example is a part of the Java HashMap tutorial with examples.
Please let me know your views in the comments section below.