← Quiz: Java Streams in Depth: all 10 questions
Quiz: Java Streams in Depth · Question 2 of 10
What happens when Collectors.toMap(key, value) meets two elements with the same key?
What happens when Collectors.toMap(key, value) meets two elements with the same key?
- A.The second value silently replaces the first
- B.The first value is kept
- C.It throws IllegalStateException
- D.Both values are stored in a list
Answer & explanation
Correct answer: It throws IllegalStateException
Without a merge function, a duplicate key is an error. Add a third argument such as Integer::sum.
Can you get the rest right without peeking?
Take the timed Quiz: Java Streams in Depth quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →