Today, I introduce you to Java 8 List to Map duplicate keys examples. Here, we will use Collectors.toMap() method to convert a List of an object into a Map. One more thing, If List has duplicates but Map doesn’t allow duplicate keys. When you are converting List to Map and what will happen?
Let’s discovery via the examples below:
1. Java 8 List to Map – Collectors.toMap()
We’re considering a list of the People objects and uses Collectors.toMap to convert it into a Map.
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 |
public class ListToMapExample { public static void main(String[] args) { List<People> list = new ArrayList<>(); list.add(new People(1, 22, "M", "Mr A")); list.add(new People(2, 22, "M", "Mr B")); list.add(new People(3, 22, "F", "Mr C")); list.add(new People(4, 22, "F", "Mr D")); list.add(new People(5, 22, "M", "Mr E")); // key = id, value - name Map<Integer, String> result1 = list.stream().sorted(Comparator.comparing(People::getName)) .collect(Collectors.toMap(People::getId, People::getName)); System.out.println("Result 1 : " + result1); // key = name, value - age Map<String, Integer> result2 = list.stream().collect(Collectors.toMap(People::getName, People::getAge)); System.out.println("Result 2 : " + result2); // Same with result1, using lambda expression // key = id, value = name Map<Integer, String> result3 = list.stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getName())); System.out.println("Result 3 : " + result3); // Same with result1, but sorting by name then reversed it // key = id, value = name Map<Integer,String> result4 = list.stream().sorted(Comparator.comparing(People::getName).reversed()).collect( Collectors.toMap(People::getId, People::getName, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); System.out.println("Result 4 : " + result4); } } |
Output
1 2 3 4 |
Result 1 : {1=Mr A, 2=Mr B, 3=Mr C, 4=Mr D, 5=Mr E} Result 2 : {Mr A=22, Mr B=22, Mr C=22, Mr D=22, Mr E=22} Result 3 : {1=Mr A, 2=Mr B, 3=Mr C, 4=Mr D, 5=Mr E} Result 4 : {5=Mr E, 4=Mr D, 3=Mr C, 2=Mr B, 1=Mr A} |
2. Java 8 List to Map duplicate keys
a) In case there are more than one objects which are duplicated key. If we try to convert them to map, a duplicated key errors will be thrown. Let’s see detail the program below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class ListToMapExample2 { public static void main(String[] args) { List list = new ArrayList<>(); list.add(new People(1,12, "M", "Mr A")); list.add(new People(2,23, "M", "Mr B")); list.add(new People(3,34, "F", "Mr C")); list.add(new People(4,45, "F", "Mr D")); list.add(new People(5,56, "M", "Mr D")); // key = name, value - age , but the key 'Mr D' is duplicated? Map<String, Integer> result1 = list.stream().collect( Collectors.toMap(People::getName, People::getAge)); System.out.println("Result 1 : " + result1); } } |
Output
b) Fix the duplicated key issue
To solve the duplicated key issue above, pass in the third mergeFunction argument such as
1 2 3 4 5 |
// key = name, value - age , but the key 'Mr D' is duplicated? // so we must pass in the third mergeFunction argument // (oldVal, newVal) -> oldVal, this returns old value Map<String, Integer> result2 = list.stream().collect( Collectors.toMap(People::getName, People::getAge,(oldVal, newVal) -> oldVal)); |
Or
1 2 3 4 5 |
// key = name, value - age , but the key 'Mr D' is duplicated? // so we must pass in the third mergeFunction argument // (oldVal, newVal) -> newVal, this returns new value Map<String, Integer> result3 = list.stream().collect( Collectors.toMap(People::getName, People::getAge,(oldVal, newVal) -> newVal)); |
c) Here is the demo program
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 |
public class ListToMapExample3 { public static void main(String[] args) { List list = new ArrayList<>(); list.add(new People(1,12, "M", "Mr A")); list.add(new People(2,23, "M", "Mr B")); list.add(new People(3,34, "F", "Mr C")); list.add(new People(4,45, "F", "Mr D")); list.add(new People(5,56, "M", "Mr D")); //Java 8 List to Map duplicate keys // key = name, value - age , but the key 'Mr D' is duplicated? Java 8 List to Map duplicate keys // so we must pass in the third mergeFunction argument // (oldVal, newVal) -> oldVal, this returns old value Map<String, Integer> result2 = list.stream().collect( Collectors.toMap(People::getName, People::getAge,(oldVal, newVal) -> oldVal)); System.out.println("Result 2 : " + result2); // key = name, value - age , but the key 'Mr D' is duplicated? // so we must pass in the third mergeFunction argument // (oldVal, newVal) -> newVal, this returns new value Map<String, Integer> result3 = list.stream().collect( Collectors.toMap(People::getName, People::getAge,(oldVal, newVal) -> newVal)); System.out.println("Result 3 : " + result3); } } |
Output
1 2 |
Result 2 : {Mr A=12, Mr B=23, Mr C=34, Mr D=45} Result 3 : {Mr A=12, Mr B=23, Mr C=34, Mr D=56} |
That’s all about the Java 8 List to Map duplicate keys. You may be interested in Java 8 Stream Distinct Examples.
References
Java 8 Collectors JavaDoc
Download the complete source code, click link below
ListToMapExample.zip (341 downloads)