Convert List to Map in Java 8 using Streams and Collectors

Converting List to Map is very common task that you encounter during programming. In this quick sneppet, you will learn how to convert List to Map using Java 8 Streams and Collectors.

Before you begin let’s build a sample POJO or element of a List. Note, in this element “id” is a unique field and we can use that for key in map.

public class Person {

	private String id;
	
	private String name;
	
	private int age;
	
	public Person(String id, String name, int age)
	{
		this.id = id;
		this.name = name;
		this.age = age;
	}
	
	public String getId() {
		return id;
	}	

        //other getters and setters
	
}

Convert List to Map before Java 8:

Before Java 8 you can use for-each loop and core java methods to convert list to map as shown below.

List<Person> personsList = new ArrayList<>();
personsList.add(new Person("1", "John", 40));
personsList.add(new Person("2", "Paul", 24));
personsList.add(new Person("3", "Samuel", 30));
personsList.add(new Person("4", "Rohit", 26));
personsList.add(new Person("5", "Krish", 18));

//before Java 8
Map<String, Person> personsMap = new HashMap<>();
		
for(Person person : personsList) {
    personsMap.put(person.getId(), person);
}

Convert List to Map Java 8:

After Java 8 you can use streams and collectors to process data in a declarative way.

//Java 8
Map<String, Person> pMap = personsList.stream()
	.collect(Collectors.toMap(Person::getId, person -> person));		

It is important for you to know that streams can leverage multi-core architectures without you having to write a single line of code for multithreading.

Complete Example:

package com.sneppets.java;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ConvertListToMap {
	
	public static void main(String[] args)
	{
		List<Person> personsList = new ArrayList<>();
		personsList.add(new Person("1", "John", 40));
		personsList.add(new Person("2", "Paul", 24));
		personsList.add(new Person("3", "Samuel", 30));
		personsList.add(new Person("4", "Rohit", 26));
		personsList.add(new Person("5", "Krish", 18));
		
		//before Java 8
		Map<String, Person> personsMap = new HashMap<>();		
		
		for(Person person : personsList) {
			personsMap.put(person.getId(), person);
		}	
		printDetails("Before Java 8", personsMap);
		
		
		//Java 8
		Map<String, Person> pMap = personsList.stream()
				.collect(Collectors.toMap(Person::getId, person -> person));		
		printDetails("After Java 8", pMap);
	}

	private static  void printDetails(String value, Map<String, Person> pMap) {
		System.out.println(value);
		for (Map.Entry<String, Person> entry : pMap.entrySet()) {
	        System.out.println(entry.getKey() + ":"
	        		+ "|" + entry.getValue().getName() + "|" + entry.getValue().getAge());
	    }
	}

}

Output:

Before Java 8
1:|John|40
2:|Paul|24
3:|Samuel|30
4:|Rohit|26
5:|Krish|18

After Java 8
1:|John|40
2:|Paul|24
3:|Samuel|30
4:|Rohit|26
5:|Krish|18

Further Learning

References

guest
0 Comments
Inline Feedbacks
View all comments