← 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?
- A.Collectors.toMap(Student::city, 1)
- B.Collectors.partitioningBy(Student::city)
- C.Collectors.mapping(Student::city, Collectors.toList())
- 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 →