Convert List to Map in Java
1. Introduction
In this article, You'll explore and learn how to convert List to Map in Java
8.
First, Let us convert List into Map.
Next, Convert List of user-defined(custom) objects to Map and handling with
the duplicate keys.
Finally, Sort and collect the Map from List
2. Collectors.toMap() Method: List<V> into Map<K, V>
Collectors is a new Java 8 utility class that provides many methods to
collect the output of stream into final List or Set or Map.
Collectors.toMap() method is a overloaded method takes 2, 3, and 4 arguments
in 3 forms.
Syntax:
public static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)public static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper,Function<? super T,? extends U> valueMapper,BinaryOperator<U> mergeFunction)public static <T,K,U,M extends Map<K,U>> Collector<T,?,M> toMap(Function<? super T,? extends K> keyMapper,Function<? super T,? extends U> valueMapper,BinaryOperator<U> mergeFunction,Supplier<M> mapSupplier)
This method is used to take the keyMapper and valueMapper Function functional interface and returns Collector object which holds our new output Map.
toMap() is to convert the given function mappers into a new Map.
3. Java 8 List To Map: Collectors.toMap()
The below program to work with the simple Strings objects to convert List to Map.
package Collectors;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.stream.Collectors;public class Java8ListToMapExample {public static void main(String[] args) {List<String> names = new ArrayList<String>();names.add("james");names.add("Cisran");names.add("Paul");System.out.println("names list values are : " + names);// Names length mapMap<String, Integer> namesLengthMap = names.stream().collect(Collectors.toMap(String::new, String::length));System.out.println("names length map : ;" + namesLengthMap);// Nanmes to upper caseMap<String, String> namesUppercaseMap = names.stream().collect(Collectors.toMap(String::new, String::toUpperCase));System.out.println("names upper case map : ;" + namesUppercaseMap);}}
Output:
names list values are : [james, Cisran, Paul]
names length map : ;{Cisran=6, james=5, Paul=4}
names upper case map : ;{Cisran=CISRAN, james=JAMES, Paul=PAUL}
4. Java 8 List<V> into Map<K, V> with Custom Objects
Creating an Employee class with id, name, salary, age, location along with constructor, setters & getter methods. This class will be used throughout this article.
class Employee {
private int id;
private String name;
private long salary;
private int age;
private String location;
public Employee(int id, String name, long salary, int age, String location) {
super();
this.id = id;
this.name = name;
this.salary = salary;
this.age = age;
this.location = location;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
Converting List<Employee> into various Map combinations.
package Collectors;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Java8ListToMapExample {
public static void main(String[] args) {
List<Employee> names = new ArrayList<Employee>();
names.add(new Employee(100, "James", 1000, 35, "USA"));
names.add(new Employee(101, "Cisran", 5000, 25, "UK"));
names.add(new Employee(102, "Paul", 7000, 40, "AUS"));
System.out.println("Employees list values are : " + names);
// Map - Id, Name
Map<Integer, String> idNameMap = names.stream().collect(Collectors.toMap(Employee::getId, Employee::getName));
System.out.println("Map <Id, Name>" + idNameMap);
// Map - Id, Salary
Map<Integer, Long> idSalaryMap = names.stream().collect(Collectors.toMap(Employee::getId, Employee::getSalary));
System.out.println("Map <Id, Salary>" + idSalaryMap);
// Map - Id, Age
Map<Integer, Integer> idAgeMap = names.stream().collect(Collectors.toMap(Employee::getId, Employee::getAge));
System.out.println("Map <Id, Age>" + idAgeMap);
// Map - Id, Location
Map<Integer, String> idLocationMap = names.stream()
.collect(Collectors.toMap(Employee::getId, Employee::getLocation));
System.out.println("Map <Id, Location>" + idLocationMap);
}
}
Output:
Employees list values are : [Collectors.Employee@60f82f98, Collectors.Employee@35f983a6, Collectors.Employee@7f690630] Map <Id, Name>{100=James, 101=Cisran, 102=Paul} Map <Id, Salary>{100=1000, 101=5000, 102=7000} Map <Id, Age>{100=35, 101=25, 102=40} Map <Id, Location>{100=USA, 101=UK, 102=AUS}
5. Java 8 List<V> into Map<K, V> with Custom Duplicate Objects Objects - MergerFunction
Now let us add the new employee with id 102 which already exists in the list.
names.add(new Employee(102, "Gena", 3000, 30, "NZ"));
Next, Run the full code as below. toMap() method, we are passing getId() as the key that means id should be unique in the list but it has duplicates for id 102. We should see the runtime exception now.
package Collectors; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Java8ListToMapExample { public static void main(String[] args) { List<Employee> names = new ArrayList<Employee>(); names.add(new Employee(100, "James", 1000, 35, "USA")); names.add(new Employee(101, "Cisran", 5000, 25, "UK")); names.add(new Employee(102, "Paul", 7000, 40, "AUS")); names.add(new Employee(102, "Gena", 3000, 30, "NZ")); // Map - Id, Name Map<Integer, String> idNameMap = names.stream().collect(Collectors.toMap(Employee::getId, Employee::getName)); System.out.println("Map <Id, Name>" + idNameMap); } }
Output:
Exception in thread "main" java.lang.IllegalStateException: Duplicate key Paul
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1254)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at Collectors.Java8ListToMapExample.main(Java8ListToMapExample.java:21)
Error is completely misleading as it is showing the Name instead of duplicate id 102.
In this case, if you know which one to ignore then we are good to use the toMap() overloaded method with mergeFunction.
Please use the below syntax and provide custom mergeFunction logic to ignore the duplicate one.
public static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
BinaryOperator functional interface takes the same type as input and returns the same type as output. This is a subinterface to BiFunction and takes two parameters.
public class Java8ListToMapExample {
public static void main(String[] args) {
List<Employee> names = new ArrayList<Employee>();
names.add(new Employee(100, "James", 1000, 35, "USA"));
names.add(new Employee(101, "Cisran", 5000, 25, "UK"));
names.add(new Employee(102, "Paul", 7000, 40, "AUS"));
names.add(new Employee(102, "Gena", 3000, 30, "NZ"));
// Map - Id, Name
Map<Integer, String> idNameMap = names.stream()
.collect(Collectors.toMap(Employee::getId, Employee::getName, (oldValue, newValue) -> newValue));
System.out.println("Map <Id, Name>" + idNameMap);
}
}
Output:
Map <Id, Name>{100=James, 101=Cisran, 102=Gena}
Name for id 102 is replaced with the new value.
Try with old value: This ignores the next duplicate occurrence.
Map<Integer, String> idNameMap = names.stream()
.collect(Collectors.toMap(Employee::getId, Employee::getName, (oldValue, newValue) -> oldValue));
Output:
Map <Id, Name>{100=James, 101=Cisran, 102=Paul}
6. Convert List to LinkedHashMap and TreeMap (Sort) with Collectors.toMap() - Supplier
As of now, we've seen how to convert List to Map and default output of key-value pair is stored into HashMap.
toMap() has another overloaded method that takes 4th argument Supplier. From the Supplier, You need to pass the LinkedHashMap or TreeMap object as shown in the below example.
package Collectors;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.TreeMap;
import java.util.stream.Collectors;
public class Java8ListToMapExample {
public static void main(String[] args) {
List<Employee> names = new ArrayList<Employee>();
names.add(new Employee(100, "James", 1000, 35, "USA"));
names.add(new Employee(101, "Cisran", 5000, 25, "UK"));
names.add(new Employee(102, "Paul", 7000, 40, "AUS"));
names.add(new Employee(102, "Gena", 3000, 30, "NZ"));
// Map - Id, Name
LinkedHashMap<Integer, String> linkedhashMap = names.stream().collect(Collectors.toMap(Employee::getId,
Employee::getName, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
System.out.println("LinkedHashMap <Id, Name>" + linkedhashMap);
// Map - Id, Name
TreeMap<String, String> treeMap = names.stream().collect(Collectors.toMap(Employee::getName,
Employee::getLocation, (oldValue, newValue) -> oldValue, TreeMap::new));
System.out.println("TreeMap <Id, Name>" + treeMap);
}
}
Output:
LinkedHashMap <Id, Name>{100=James, 101=Cisran, 102=Paul} TreeMap <Id, Name>{Cisran=UK, Gena=NZ, James=USA, Paul=AUS}
From the output, First LinkedHashMap stored 3 key-value pairs whereas TreeMap has 4 key-value pairs because the key is an emp name which is not having the duplicates names.
7. Conclusion
In this article, You've seen in-depth about how to convert List to Map in Java 8. How to deal with Custom objects in List and convert them into Map.
How to handle the duplicate custom objects if List is having?
By default, toMap() method converts List into HashMap but there are some cases you may encounter to convert to LinkedHashMap or TreeMap. To convert to your own map, you need to provide the Supplier to toMap() function.
As usual, All examples are shown over the GitHub.
No comments:
Post a Comment
Please do not add any spam links in the comments section.