1Z0-809 · Question #99
Given the definition of the Country class: public class Country { public enum Continent {ASIA, EUROPE} String name; Continent region; public Country (String na, Continent reg) { name = na; region =…
The correct answer is A. {EUROPE = [Italy, Germany], ASIA = [Japan]}. Option A is correct because Collectors.groupingBy uses a HashMap internally (no guaranteed key order), and Collectors.mapping preserves encounter order within each group - Italy appears before Germany in the original list, so the EUROPE list is [Italy, Germany], not [Germany…
Question
Options
- A{EUROPE = [Italy, Germany], ASIA = [Japan]}
- B{ASIA = [Japan], EUROPE = [Italy, Germany]}
- C{EUROPE = [Germany, Italy], ASIA = [Japan]}
- D{EUROPE = [Germany], EUROPE = [Italy], ASIA = [Japan]}
How the community answered
(46 responses)- A83% (38)
- B2% (1)
- C11% (5)
- D4% (2)
Explanation
Option A is correct because Collectors.groupingBy uses a HashMap internally (no guaranteed key order), and Collectors.mapping preserves encounter order within each group - Italy appears before Germany in the original list, so the EUROPE list is [Italy, Germany], not [Germany, Italy]. Option C is wrong for exactly this reason: it reverses Italy and Germany within the EUROPE group, violating encounter order. Option D is wrong because a Map cannot have duplicate keys - groupingBy merges all entries sharing a key into a single List, not separate entries. Option B has the correct values but shows ASIA before EUROPE; while HashMap order is technically undefined, exam questions typically expect the output that the JVM produces for the given hash codes, which puts EUROPE first here. Memory tip: Think of groupingBy + mapping as "sort into buckets, then transform what's in each bucket" - the bucket contents always stay in the order they were encountered in the stream.
Community Discussion
No community discussion yet for this question.