nerdexam
Oracle

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

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 = reg; } } and the code fragment: public String getName() {return name;} public Continent getRegion() {return region;} List<Country> couList = Arrays.asList( new Country ("Japan", Country.Continent.ASIA), new Country ("Italy", Country.Continent.EUROPE), new Country ("Germany", Country.Continent.EUROPE)); Map<Country.Continent, List<String>> regionNames = couList.stream () .collect(Collectors.groupingBy (Country::getRegion, Collectors.mapping (Country::getName, Collectors.toList ()))); System.out.println(regionNames); What is the output?

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)
  • A
    83% (38)
  • B
    2% (1)
  • C
    11% (5)
  • D
    4% (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.

Full 1Z0-809 Practice