category

Bugs.

How to handle duplicates Keys when producing map using streams?

Himanshu Ranjan

Problem:

Map<String, String> phoneBook = people.stream()
                                      .collect(toMap(Person::getName,
                                                     Person::getAddress));

I get java.lang.IllegalStateException: Duplicate key when a duplicated element is found.

Is it possible to ignore or handle such exception on adding values to the map?

**Solution:*...

20 reads