This question already has an answer here:
- Why does Collectors.toMap report value instead of key on Duplicate Key error? 4 answers
- Java 8 toMap IllegalStateException Duplicate Key 2 answers
- Ignore duplicates when producing map using streams 6 answers
- Alternative for throwingMerger in Java 8 1 answer
- Collectors.toMap with same keys(print same key) 2 answers
Got this exception:
java.lang.IllegalStateException: Duplicate key 50
I looked over every map that is using that code, and there is no such key It took me a while but I found the problem but it is confusing and very problematic to understand don't know why they did it like this
this is my code:
List<Person> listOfPeople = new LinkedList<Person>();
Map<String, Integer> myMap = listOfPeople
.stream()
.collect(Collectors.toMap(
Person::getNameInString,
Person::getAgeInInt
)
);
My map is String to Integer, so were did 50 came from???