🎙 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 3 of 10

Which collector gives, for each city, how many students live there?

Which collector gives, for each city, how many students live there?

  1. A.Collectors.toMap(Student::city, 1)
  2. B.Collectors.partitioningBy(Student::city)
  3. C.Collectors.mapping(Student::city, Collectors.toList())
  4. D.Collectors.groupingBy(Student::city, Collectors.counting())
Answer & explanation

Correct answer: Collectors.groupingBy(Student::city, Collectors.counting())

groupingBy makes city buckets; counting turns each bucket into its size.

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 →