Given the definition of the Country class: public class…

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;
}
public String getName () {return name;}
public Continent getRegion () {return region;}
}and the code fragment:
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);

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;
}
public String getName () {return name;}
public Continent getRegion () {return region;}
}and the code fragment:
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);

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]}



Leave a Reply 7

Your email address will not be published. Required fields are marked *


smh

smh

A

DG

DG

Actually with Japan being the first one on the list, on my machine it’s always
{ASIA=[Japan], EUROPE=[Italy, Germany]}
(option B)

Yet I wonder why do they differentiate options A and B, because as Collectors.groupingBy creates a HashMap which does not guarantee its order, it could as well have been the option A.

smh

smh

Sorry, correct answer is B.

Berti John

Berti John

A. is correct

smh

smh

B.
run:
{ASIA=[Japan], EUROPE=[Italy, Germany]}
BUILD SUCCESSFUL