This example shows how to convert List to Map in Java using various ways including Java 8. The example also shows how to convert ArrayList or LinkedList to HashMap having duplicate elements.
How to convert List to Map in Java?
The List and Map are two different types of collections. The List is an ordered collection of the same type of elements while the Map maintains a mapping of a key to a value. There is no direct way to convert from one to another.
In the below given example, I will show you how to convert a List containing the objects of the Employee class to a HashMap object.
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; class Employee{ private Integer ID; private String name; public Employee(){} public Employee(Integer ID, String name){ this.ID = ID; this.name = name; } public Integer getID() { return ID; } public void setID(Integer iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString(){ return "[" + getID() + "->" + getName() + "]"; } } public class ListToHashMapExample { public static void main(String[] args) { //the List object of Employee objects List<Employee> listEmployees = new ArrayList<Employee>(); listEmployees.add( new Employee(1, "Aakash") ); listEmployees.add( new Employee(2, "John") ); listEmployees.add( new Employee(3, "Emily") ); listEmployees.add( new Employee(4, "Jack") ); listEmployees.add( new Employee(5, "Mary") ); System.out.println("List contains: " + listEmployees); /* * To convert List to HashMap, iterate over * the list and add the elements to the HashMap * using the put method */ //map object Map<Integer, Employee> mapEmployees = new HashMap<Integer, Employee>(); //iterate over the list for( Employee employee : listEmployees ){ /* * HashMap is a key-value mapping, so we are going * to add id as a key and Employee object as value * to the map object */ mapEmployees.put( employee.getID(), employee); } System.out.println("HashMap contains: " + mapEmployees); } } |
Output
1 2 |
List contains: [[1->Aakash], [2->John], [3->Emily], [4->Jack], [5->Mary]] HashMap contains: {1=[1->Aakash], 2=[2->John], 3=[3->Emily], 4=[4->Jack], 5=[5->Mary]} |
As you can see from the output, the HashMap has the employee ID as the key and Employee object as the value.
Important Tip:
The HashMap class does not guarantee to maintain the insertion order of the mappings. If the order of the mappings is important for you, you can use the LinkedHashMap class instead of the HashMap class.
How to convert List to HashMap using the Java 8 stream?
If you are using Java version 8 or later, you can use the stream to convert List to Map as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//the List object of Employee objects List<Employee> listEmployees = new ArrayList<Employee>(); listEmployees.add( new Employee(1, "Aakash") ); listEmployees.add( new Employee(2, "John") ); listEmployees.add( new Employee(3, "Emily") ); listEmployees.add( new Employee(4, "Jack") ); listEmployees.add( new Employee(5, "Mary") ); System.out.println("List contains: " + listEmployees); Map<Integer, Employee> mapEmployees = listEmployees.stream().collect( Collectors.toMap(Employee::getID, employee -> employee)); System.out.println("HashMap contains: " + mapEmployees); |
Output
1 2 |
List contains: [[1->Aakash], [2->John], [3->Emily], [4->Jack], [5->Mary]] HashMap contains: {1=[1->Aakash], 2=[2->John], 3=[3->Emily], 4=[4->Jack], 5=[5->Mary]} |
This example is a part of the Java HashMap tutorial.
Please let me know your views in the comments section below.