In this post, we will learn How to convert List to Map using Java 8 Stream API, where a key is any attribute from the object and value is the object itself or any attribute from Object.
Let’s clarify it using the below example.
Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.kkjavatutorials.model; public class Employee { private int id; private String name; private boolean isFemale; private boolean isMale; private Double salary; public Employee(int id, String name, boolean isFemale, boolean isMale, Double salary) { super(); this.id = id; this.name = name; this.isFemale = isFemale; this.isMale = isMale; this.salary = salary; } public int getId() { return id; } public String getName() { return name; } public boolean isFemale() { return isFemale; } public boolean isMale() { return isMale; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", isFemale=" + isFemale + ", isMale=" + isMale + ", salary=" + salary + "]"; } } |
StreamApiExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package com.kkjavatutorials.client; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import com.kkjavatutorials.model.Employee; /** * @author KK JavaTutorials *Convert List to Map using java 8 Stream API */ public class StreamApiExample { public static void main(String[] args) { Employee employee1 = new Employee(1002, "Sean", false, true, 10000.00); Employee employee2 = new Employee(1072, "Marry", true, false, 15000.00); Employee employee3 = new Employee(1882, "Sam", false, true, 30000.00); Employee employee4 = new Employee(1992, "Sofiya", true, false, 3000.00); Employee employee5 = new Employee(1112, "Raj", false, true, 16000.00); Employee employee6 = new Employee(1772, "Hema", true, false, 9000.00); //List of employees List<Employee> empList = new ArrayList<>(); empList.add(employee1); empList.add(employee2); empList.add(employee3); empList.add(employee4); empList.add(employee5); empList.add(employee6); System.out.println("-------Getting Map from List where key = id, value = name in Map using Stream API----------"); // key = id, value = name Map<Integer, String> empIdAndNameMap = empList.stream().collect( Collectors.toMap(Employee::getId, Employee::getName)); System.out.println("empIdAndNameMap : " + empIdAndNameMap); // Same as empIdAndNameMap but different syntax // key = id, value = name Map<Integer, String> empIdAndNameMap2 = empList.stream().collect( Collectors.toMap(e -> e.getId(), e -> e.getName())); System.out.println("empIdAndNameMap2 : " + empIdAndNameMap2); System.out.println("-------Getting Map from List where key = id, value = salary in Map using Stream API----------"); // key = name, value = salary Map<String, Double> empNameAndSalaryMap = empList.stream().collect( Collectors.toMap(Employee::getName, Employee::getSalary)); System.out.println("empNameAndSalaryMap : " + empNameAndSalaryMap); System.out.println("-------Getting Map from List where key = id, value = Employee Object in Map using Stream API----------"); //key = id, value = Employee Object Map<Integer, Employee> collect = empList.stream().collect(Collectors.toMap(e->e.getId(),e->e)); System.out.println(collect); } } |
Output of this program:
——-Getting Map from List where key = id, value = name in Map using Stream API———-
empIdAndNameMap : {1072=Marry, 1112=Raj, 1992=Sofiya, 1882=Sam, 1002=Sean, 1772=Hema}
empIdAndNameMap2 : {1072=Marry, 1112=Raj, 1992=Sofiya, 1882=Sam, 1002=Sean, 1772=Hema}
——-Getting Map from List where key = id, value = salary in Map using Stream API———-
empNameAndSalaryMap : {Hema=9000.0, Sean=10000.0, Sofiya=3000.0, Raj=16000.0, Sam=30000.0, Marry=15000.0}
——-Getting Map from List where key = id, value = Employee Object in Map using Stream API———-
{1072=Employee [id=1072, name=Marry, isFemale=true, isMale=false, salary=15000.0], 1112=Employee [id=1112, name=Raj, isFemale=false, isMale=true, salary=16000.0], 1992=Employee [id=1992, name=Sofiya, isFemale=true, isMale=false, salary=3000.0], 1882=Employee [id=1882, name=Sam, isFemale=false, isMale=true, salary=30000.0], 1002=Employee [id=1002, name=Sean, isFemale=false, isMale=true, salary=10000.0], 1772=Employee [id=1772, name=Hema, isFemale=true, isMale=false, salary=9000.0]}
You May Also Like:
Explain Stream API introduced in Java 8 ?
There is a stream of words which contains Anagrams. How would you print anagrams in a single bucket from that stream?
Java Spliterator Example
Differences Between map() And flatMap() in Java 8 ?
What are the new features added in Java 8?
That’s all in this post of How to convert List to Map using Java
If you have any feedback or suggestion please feel free to drop in below comment box.