🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.The second value silently replaces the first
  2. B.The first value is kept
  3. C.It throws IllegalStateException
  4. 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 →