Conversion of List to Map in Java

In this article, we will discuss how to convert List into Map in Java

 

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.

 

We will cover 2 examples as described below,

  1. Id can be chosen as key and any other attribute like name can selected for value
  2. 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;
	}

	// getter & setter
	public int getEmpId() {
		return empId;
	}
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	public String getEmpName() {
		return empName;
	}
	public void setEmpName(String empName) {
		this.empName = empName;
	}
	public int getEmpAge() {
		return empAge;
	}
	public void setEmpAge(int empAge) {
		this.empAge = empAge;
	}
	public String getEmpDesignation() {
		return empDesignation;
	}
	public void setEmpDesignation(String empDesignation) {
		this.empDesignation = empDesignation;
	}

	// override toString()
	@Override
	public String toString() {
		return "Employee [empId=" + empId + ", empName=" + empName
				+ ", empAge=" + empAge + ", empDesignation=" + empDesignation
				+ "]";
	}
}

 

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

 

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/7/docs/api/java/util/ArrayList.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/7/docs/api/java/util/HashMap.html
https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

 

Read Also:

 

Happy Coding !!
Happy Learning !!

Conversion of List to Map in Java 8
Conversion of Map to List in Java 8