In this article, we will discuss how to convert List into Map in Java
1. List v/s Map:
Before starting with List to Map conversion, we should understand difference between List and Map
- List stores group of objects as single unit/entity where it allows duplicates objects maintaining insertion order
- Map stores group of key-value pairs as single unit/entity where keys must be unique and values can be duplicated
So, we need to decide what we want to put inside Map as key and value. Ideally, whenever list stores group of some objects having 2 or more attributes then we can simply choose any 2-attributes for key and value while putting inside Map.
2. Conversion of List to Map:
We will cover 2 examples as described below,
- Id can be chosen as key and any other attribute like name can selected for value
- Again Id can be chosen as key and whole object as value
Let’s move forward and implement above examples
Employee POJO:
Employee with 4-attributes namely,
- Id
- name
- age
- designation
Employee.java
package in.bench.resources.list.to.map;
public class Employee {
// member variables
private int empId;
private String empName;
private int empAge;
private String empDesignation;
// 4-arg parameterized constructor
public Employee(int empId, String empName,
int empAge, String empDesignation) {
super();
this.empId = empId;
this.empName = empName;
this.empAge = empAge;
this.empDesignation = empDesignation;
}
// getters and setters
// override toString()
@Override
public String toString() {
return "Employee ["
+ "empId=" + empId
+ ", empName=" + empName
+ ", empAge=" + empAge
+ ", empDesignation=" + empDesignation
+ "]";
}
}
2.1 Convert List to Map with 2-attributes of POJO as Key-Value pair
Here,
- Id is selected as key
- name for value
- refer above Employee POJO for attribute details
ConvertListToMap.java
package in.bench.resources.list.to.map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ConvertListToMap {
public static void main(String[] args) {
// create ArrayList
List<Employee> employees = new ArrayList<Employee>();
// add employee objects to list
employees.add(new Employee(1001, "SJ", 18, "Consultant"));
employees.add(new Employee(1002, "AK", 20, "Enginner"));
employees.add(new Employee(1003, "PJ", 23, "Developer"));
// printing to console - List of Employee
System.out.println("List of Employee : \n"
+ employees);
// create Map<Integer, String>
// to store List of employees
Map<Integer, String> mapOfEmployees =
new HashMap<Integer, String>();
// convert List<Employee> to Map<empId, empName>
for(Employee employee : employees) {
mapOfEmployees.put(
employee.getEmpId(), employee.getEmpName());
}
// printing to console - Map of Employee
System.out.println("\n\nMap of Employee : \n"
+ mapOfEmployees);
}
}
Output:
List of Employee :
[
Employee [empId=1001, empName=SJ, empAge=18, empDesignation=Consultant],
Employee [empId=1002, empName=AK, empAge=20, empDesignation=Enginner],
Employee [empId=1003, empName=PJ, empAge=23, empDesignation=Developer]
]
Map of Employee :
{1001=SJ, 1002=AK, 1003=PJ}
Note: results in the output console prints in single line, for better understanding (or viewing) purpose it is formatted
2. Convert List to Map with primary Id as Key and complete object as value
Again,
- Id is selected as key
- whole object for value
- refer above Employee POJO for attribute details
ConvertListToMapOfEmployee.java
package in.bench.resources.list.to.map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ConvertListToMapOfEmployee {
public static void main(String[] args) {
// create ArrayList
List<Employee> employees = new ArrayList<Employee>();
// add employee objects to list
employees.add(new Employee(1001, "SJ", 18, "Consultant"));
employees.add(new Employee(1002, "AK", 20, "Enginner"));
employees.add(new Employee(1003, "PJ", 23, "Developer"));
// printing to console - List of Employee
System.out.println("List of Employee : \n"
+ employees);
// create Map<Integer, Employee>
// to store List of employees
Map<Integer, Employee> mapOfEmployees =
new HashMap<Integer, Employee>();
// convert List<Employee> to Map<empId, empName>
for(Employee employee : employees) {
mapOfEmployees.put(employee.getEmpId(), employee);
}
// printing to console - Map of Employee
System.out.println("\n\nMap of Employee : \n"
+ mapOfEmployees);
}
}
Output:
List of Employee :
[
Employee [empId=1001, empName=SJ, empAge=18, empDesignation=Consultant],
Employee [empId=1002, empName=AK, empAge=20, empDesignation=Enginner],
Employee [empId=1003, empName=PJ, empAge=23, empDesignation=Developer]
]
Map of Employee :
{
1001=Employee [empId=1001, empName=SJ, empAge=18, empDesignation=Consultant],
1002=Employee [empId=1002, empName=AK, empAge=20, empDesignation=Enginner],
1003=Employee [empId=1003, empName=PJ, empAge=23, empDesignation=Developer]
}
Note: results in the output console prints in single line, for better understanding (or viewing) purpose it is formatted
In the following article, we will see how we could achieve same result using Java 8 version
Related Articles:
- Java – Conversion of List to Map
- Java – Conversion of Map to List
- Java – Conversion of Arrays to List
- Java – Conversion of List to Arrays
- Java 8 – Conversion of List to Map
- Java 8 – Conversion of Map to List
- Java 8 – Conversion of Arrays to List
- Java 8 – Conversion of List to Arrays
- Java 8 – How to convert HashMap to ArrayList ?
- Java 8 – Conversion of Arrays to Stream
- Java 8 – Conversion of Stream to Arrays
- Java 8 – Convert List to Stream
- Java 8 – Convert Stream to List
- Java 8 – Convert Stream to ArrayList
- Java 8 – Convert Stream to LinkedList
- Java 8 – Convert Stream to HashSet
- Java 8 – Convert Stream to LinkedHashSet
- Java 8 – Convert Stream to TreeSet
- Java 8 – Convert Stream to HashMap
- Java 8 – Convert Stream to LinkedHashMap
- Java 8 – Convert Stream to TreeMap
- Java 8 – Convert Stream to ConcurrentHashMap
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/tutorial/collections/implementations/list.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
Happy Coding !!
Happy Learning !!