Converting List to Map is a common task. In this example, we’ll cover converting List to Map before Java 8 and after Java 8.
We’ll assume that each element of the List has an identifier which will be used as a key in the resulting Map.
The id field is unique, hence we can make it the key.
Let’s start converting with the traditional way.
We’ll assume that each element of the List has an identifier which will be used as a key in the resulting Map.
Employee
Firstly, let’s model the element:class Employee { private Long id; private String name; public Employee(Long id, String name) { super(); this.id = id; this.name = name; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
The id field is unique, hence we can make it the key.
Let’s start converting with the traditional way.
Before Java 8
Evidently, we can convert a List to a Map using core Java methods:public Map < Long, Employee > convertListBeforeJava8(List < Employee > list) { Map < Long, Employee > map = new HashMap < > (); for (Employee animal: list) { map.put(animal.getId(), animal); } return map; }
With Java 8
Starting with Java 8 we can convert a List into a Map using streams and Collectors:public Map < Long, Employee > convertListAfterJava8(List < Employee > list) { Map < Long, Employee > map = list.stream().collect(Collectors.toMap(Employee::getId, animal -> animal)); return map; }
Complete Example
package net.javaguides.hibernate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ConvertListToMapService {
public Map < Long, Employee > convertListBeforeJava8(List < Employee > list) {
Map < Long, Employee > map = new HashMap < > ();
for (Employee animal: list) {
map.put(animal.getId(), animal);
}
return map;
}
public Map < Long, Employee > convertListAfterJava8(List < Employee > list) {
Map < Long, Employee > map = list.stream().collect(Collectors.toMap(Employee::getId, animal -> animal));
return map;
}
public static void main(String[] args) {
ConvertListToMapService convertListToMapService = new ConvertListToMapService();
List < Employee > list = new ArrayList < > ();
list.add(new Employee(100 L, "Ramesh"));
list.add(new Employee(101 L, "Tom"));
list.add(new Employee(102 L, "Tony"));
Map < Long, Employee > map = convertListToMapService.convertListBeforeJava8(list);
System.out.println(map);
Map < Long, Employee > map1 = convertListToMapService.convertListAfterJava8(list);
System.out.println(map1);
}
}
class Employee {
private Long id;
private String name;
public Employee(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Output
{100=net.javaguides.hibernate.Employee@7852e922, 101=net.javaguides.hibernate.Employee@4e25154f,
102=net.javaguides.hibernate.Employee@70dea4e}
{100=net.javaguides.hibernate.Employee@7852e922, 101=net.javaguides.hibernate.Employee@4e25154f,
102=net.javaguides.hibernate.Employee@70dea4e}
Collections Examples
- Java LinkedHashMap Example
- Java HashSet Example
- Java LinkedList Example
- Java ArrayList Example
- Java Comparator Interface Example
- Java Comparable Interface Example
- Java IdentityHashMap Example
- Java WeakHashMap Example
- Java EnumMap Example
- Java CopyOnWriteArraySet Example
- Java EnumSet Class Example
- Guide to Java 8 forEach Method
- Different Ways to Iterate over a List in Java [Snippet]
- Different Ways to Iterate over a Set in Java [Snippet]
- Different Ways to Iterate over a Map in Java [Snippet]
- Iterate over TreeSet in Java Example
- Iterate over LinkedHashSet in Java Example
- Remove First and Last Elements of LinkedList in Java
- Iterate over LinkedList using an Iterator in Java
- Search an Element in an ArrayList in Java
- Iterate over ArrayList using Iterator in Java
- Remove Element from HashSet in Java
- Iterating over a HashSet using Iterator
- How To Remove Duplicate Elements From ArrayList In Java?
- Different Ways to Iterate over List, Set, and Map in Java
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course