一张地图包括国家名称作为关键字,城市列表作为值。
List inCities = new ArrayList(Arrays.asList("Delhi", "Mumbai", "Hyderabad", "Banglore", "Chennai"));
List jpCities = new ArrayList(Arrays.asList("Tokyo", "Osaka", "Kyoto"));
List usCities = new ArrayList(Arrays.asList("Dallas", "Chicago", "NewYork"));
List ukCities = new ArrayList(Arrays.asList("London", "Cardiff", "Oxford"));
List frCities = new ArrayList(Arrays.asList("Paris", "Marseille", "Lyon"));
Map> countryWiseCities = new HashMap>();
countryWiseCities.put("India", inCities);
countryWiseCities.put("Japan", jpCities);
countryWiseCities.put("USA", usCities);
countryWiseCities.put("UK", ukCities);
countryWiseCities.put("France", frCities);
List asiaCountries = new ArrayList(Arrays.asList("India", "Japan"));
List northAmericaCountries = new ArrayList(Arrays.asList("USA", "Canada"));
List europeCountries = new ArrayList(Arrays.asList("UK", "France"));
Map> continentWiseCountries = new HashMap>();
continentWiseCountries.put("Asia", asiaCountries);
continentWiseCountries.put("NorthAmerica", northAmericaCountries);
continentWiseCountries.put("Europe", europeCountries);
现有地图。
{"India":["Delhi", "Mumbai", "Hyderabad", "Banglore", "Chennai"], "Japan":["Tokyo", "Osaka", "Kyoto"], "USA":["Dallas", "Chicago", "NewYork"], "Canada":["Ontario", "Toronto", "vancouver"],
"UK":["London", "Cardiff", "Oxford"], "France":["Paris", "Marseille", "Lyon"]}
{"Asia":["India", "Japan"], "NorthAmerica":["USA", "Canada"], "Europe":["UK", "France"]}
我正在寻找一种使用java流在地图下方构建的方法。使用Java流实现以下输出的最简单方法是什么?。
预期产量:
{"ASIA":["Delhi", "Mumbai", "Hyderabad", "Banglore", "Chennai", "Tokyo", "Osaka", "Kyoto"],
"NorthAmerica":["Dallas", "Chicago", "NewYork", "Ontario", "Toronto", "vancouver"],
"Europe":["London", "Cardiff", "Oxford", "Paris", "Marseille", "Lyon"]
这里的关键是大陆名。
下面是我尝试过的代码。
Map>>> collect1 = countryWiseCities.entrySet().stream()
.collect(Collectors.groupingBy(Map.Entry::getKey));