HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Java 8 – Convert stream to Map

By Lokesh Gupta | Filed Under: Java 8

Learn to collect stream elements into Map using Collectors.toMap() and Collectors.groupingBy() methods using Java 8 Stream APIs. Convert stream to map using Java stream APIs.

1. Stream Elements with unique map keys – Collectors.toMap()

If the stream elements have the unique map key field then we can use Collectors.toMap() to collect elements to map in Map<keyObj, Element> format easily.

For example, we can collect a list of Employee objects to map in where employee ids are unique fields and used a key to map entries.

Java 8 example to convert list to map using stream APIs.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Main 
{
	public static void main(String[] args) 
	{
		List<Employee> employeeList = new ArrayList<>(Arrays.asList(
							new Employee(1, "A", 100),
							new Employee(2, "A", 200),
							new Employee(3, "B", 300),
							new Employee(4, "B", 400),
							new Employee(5, "C", 500),
							new Employee(6, "C", 600)));
		
		Map<Long, Employee> employeesMap = employeeList.stream()
								.collect( Collectors.toMap(Employee::getId, 
										Function.identity()) );
		
		System.out.println(employeesMap);
	}
}

Program output.

{1=Employee [id=1, name=A, salary=100.0], 
2=Employee [id=2, name=A, salary=200.0], 
3=Employee [id=3, name=B, salary=300.0], 
4=Employee [id=4, name=B, salary=400.0], 
5=Employee [id=5, name=C, salary=500.0], 
6=Employee [id=6, name=C, salary=600.0]}

2. Stream Elements with duplicate map keys – Collectors.groupingBy()

If the stream elements have elements where map keys are duplicate the we can use Collectors.groupingBy() to collect elements to map in Map<keyObj, List<Element>> format. Here for each map key, we will store all elements in a list as map value.

For example, we can collect a list of Employee objects to map in where employee names may be duplicate fields for some stream elements. In such case, all employees with same name will be stored in a list, and list will be stored as map value field.

Java 8 example to convert list to map of lists using stream APIs.

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

public class Main 
{
	public static void main(String[] args) 
	{
		List<Employee> employeeList = new ArrayList<>(Arrays.asList(
							new Employee(1, "A", 100),
							new Employee(2, "A", 200),
							new Employee(3, "B", 300),
							new Employee(4, "B", 400),
							new Employee(5, "C", 500),
							new Employee(6, "C", 600)));
		
		Map<String, List<Employee>> employeesMap = employeeList.stream()
								.collect(Collectors.groupingBy(Employee::getName));
		
		System.out.println(employeesMap);
	}
}

Program output.

{A=[Employee [id=1, name=A, salary=100.0], Employee [id=2, name=A, salary=200.0]], 
 B=[Employee [id=3, name=B, salary=300.0], Employee [id=4, name=B, salary=400.0]], 
 C=[Employee [id=5, name=C, salary=500.0], Employee [id=6, name=C, salary=600.0]]}

3. Conclusion

It is very important to know beforehand if the Stream elements will have the distinct value for the map key field or not. If map keys are duplicate and we use Collectors.toMap() method, we will get the IllegalStateException:

Exception in thread "main" java.lang.IllegalStateException: Duplicate key Employee [id=5, name=C, salary=500.0]
	at java.util.stream.Collectors.lambda$throwingMerger$106(Collectors.java:133)
	at java.util.stream.Collectors$$Lambda$3/149928006.apply(Unknown Source)
	at java.util.HashMap.merge(HashMap.java:1245)

Happy Learning !!

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

2
Leave a Reply

This comment form is under antispam protection
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Buc

If you wish to create map from list by id, but list contains duplicates you can use Collectors.toMap with BinnaryOperator:

 
public static void main (String[] args) {
  List<Employee> employeeList = Arrays.asList(
                new Employee(1, "A", 100),
                new Employee(1, "A", 100),
                new Employee(2, "A", 200),
                new Employee(3, "B", 300),
                new Employee(4, "B", 400),
                new Employee(5, "C", 500),
                new Employee(5, "C", 500),
                new Employee(6, "C", 600));


        Map<Long, Employee> employeesMap = employeeList.stream()
                .collect(Collectors.toMap(Employee::getId,
                        Function.identity(), (first, second) -> first));

        employeesMap.entrySet().forEach(System.out::println);
} 

The console output will be:

 
1=Employee[id=1, name='A', salary=100.0]
2=Employee[id=2, name='A', salary=200.0]
3=Employee[id=3, name='B', salary=300.0]
4=Employee[id=4, name='B', salary=400.0]
5=Employee[id=5, name='C', salary=500.0]
6=Employee[id=6, name='C', salary=600.0]
Vote Up0Vote Down  Reply
1 month ago
Lokesh Gupta

Thanks for sharing.

Vote Up0Vote Down  Reply
1 month ago

Search Tutorials

Java 8 Tutorial

  • Java 8 – Introduction
  • Java 8 – forEach
  • Java 8 – Stream
  • Java 8 – Boxed Stream
  • Java 8 – Lambda Expression
  • Java 8 – Functional Interface
  • Java 8 – Method References
  • Java 8 – Default Method
  • Java 8 – Optionals
  • Java 8 – Predicate
  • Java 8 – Date Time
  • Java 8 – Iterate Directory
  • Java 8 – Read File
  • Java 8 – Write to File
  • Java 8 – WatchService
  • Java 8 – String to Date
  • Java 8 – Join Array
  • Java 8 – Base64
  • Java 8 – Exact Arithmetic
  • Java 8 – Comparator
  • Java 8 – Regex as Predicate
  • Java 8 – Join String
  • Java 8 – Difference Between Dates
  • Internal vs. External Iteration
  • Java 8- SecureRandom

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz