使用java流生成对象

dfty9e19  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(347)

我有一个关于java流的问题。我在Map下面建的。
一张Map包括国家名称作为关键,城市列表作为价值。
另一个包括大陆名称作为关键,国家列表作为价值。

List<String> inCities = new ArrayList<String>(Arrays.asList("Delhi", "Mumbai", "Hyderabad", "Banglore", "Chennai"));
List<String> jpCities = new ArrayList<String>(Arrays.asList("Tokyo", "Osaka", "Kyoto"));
List<String> usCities = new ArrayList<String>(Arrays.asList("Dallas", "Chicago", "NewYork"));
List<String> ukCities = new ArrayList<String>(Arrays.asList("London", "Cardiff", "Oxford"));
List<String> frCities = new ArrayList<String>(Arrays.asList("Paris", "Marseille", "Lyon"));

Map<String, List<String>> countryWiseCities = new HashMap<String, List<String>>();
countryWiseCities.put("India", inCities);
countryWiseCities.put("Japan", jpCities);
countryWiseCities.put("USA", usCities);
countryWiseCities.put("UK", ukCities);
countryWiseCities.put("France", frCities);

List<String> asiaCountries = new ArrayList<String>(Arrays.asList("India", "Japan"));
List<String> northAmericaCountries = new ArrayList<String>(Arrays.asList("USA", "Canada"));
List<String> europeCountries = new ArrayList<String>(Arrays.asList("UK", "France"));
Map<String, List<String>> continentWiseCountries = new HashMap<String, List<String>>();
continentWiseCountries.put("Asia", asiaCountries);
continentWiseCountries.put("NorthAmerica", northAmericaCountries);
continentWiseCountries.put("Europe", europeCountries);

现有Map。

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

我正在寻找一种方法来建立下面的Map使用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<String, List<Map.Entry<String, List<String>>>> collect1 = countryWiseCities.entrySet().stream()
                .collect(Collectors.groupingBy(Map.Entry::getKey));
e0uiprwp

e0uiprwp1#

这里是一个没有中间流的解决方案,只是在tomap收集器的value函数中使用flatmap:

Map<String, List<String>> continentCityMap = continentWiseCountries.entrySet().stream()
        .collect(Collectors.toMap(Entry::getKey,
                e -> e.getValue().stream().flatMap(
                        country -> countryWiseCities.getOrDefault(country, Collections.emptyList()).stream())
                        .collect(Collectors.toList())));
hwazgwia

hwazgwia2#

你可以做一件事:

Map<String, List<String>> continentCities = continentWiseCountries.entrySet()
    .stream()
    .map(continent -> new AbstractMap.SimpleEntry<>(
            continent.getKey(), 
            continent.getValue()
            .stream()
            .flatMap(country -> 
                        countryWiseCities.getOrDefault(country, Collections.emptyList())
                            .stream())
            .collect(Collectors.toList())))
    .collect(Collectors.toMap(Entry::getKey, Entry::getValue));

它所做的只是使用一个中间流从大陆连接到城市(通过国家),然后收集城市和大陆对。
请注意 getOrDefault(country, Collections.emptyList()) 是必需的,因为您的大陆列表中的国家/地区在您的国家/地区列表Map中不存在。
上述代码的输出如下:

{
  "Asia" : [ "Delhi", "Mumbai", "Hyderabad", "Banglore", "Chennai", "Tokyo", "Osaka", "Kyoto" ],
  "Europe" : [ "London", "Cardiff", "Oxford", "Paris", "Marseille", "Lyon" ],
  "NorthAmerica" : [ "Dallas", "Chicago", "NewYork" ]
}

相关问题