Java流:groupingBy和flatMapping键

时间:2019-03-26 15:04:45

标签: java java-stream

说我有一个国家对象列表,其中包含该国家所说的语言列表,例如:

class Country {
    List<String> languages; 
}

我想以以下格式创建地图: Map<String, List<Country>>,这样每种语言都可以映射到Country对象列表。例如:

"French" -> [Country:France, Country:Canada],
"English" -> [Country:UK, Country:US]

这里的性能是一个问题,因此我想避免多次迭代和查找。我已经尝试过使用groupingBy,但是如何flatMap键集?

例如,结果为Map<List<String>, List<Country>>

countries.stream()
    .collect(Collectors.groupingBy(country -> country.getLanguages(), toList()));

3 个答案:

答案 0 :(得分:1)

由于您似乎在乎性能,因此请勿将流用于此简单任务:

Map<String, List<Country>> countriesByLanguage = new HashMap<>();
for (Country country : countries) {
    for (String language : country.getLanguages()) {
        countriesByLanguage.computeIfAbsent(language, k -> new ArrayList<>())
                           .add(country);
    }
}

答案 1 :(得分:1)

您可以使用流中的流来完成它,如下所示:首先迭代国家列表,然后迭代嵌套的语言列表并准备 «language, country» 对,然后然后收集它们以映射:

public static void main(String[] args) {
    List<Country> countries = List.of(
            new Country("France", List.of("French")),
            new Country("Canada", List.of("French")),
            new Country("UK", List.of("English")),
            new Country("US", List.of("English")));

    Map<String, List<Country>> map = countries.stream()
            // Stream<Map.Entry<String,Country>>
            .flatMap(country -> country.getLanguages().stream()
                    .map(lang -> Map.entry(lang, country)))
            .collect(Collectors.toMap(
                    // key - language
                    Map.Entry::getKey,
                    // value - List<Country>
                    entry -> new ArrayList<>(List.of(entry.getValue())),
                    // merge duplicates, if any
                    (list1, list2) -> {
                        list1.addAll(list2);
                        return list1;
                    }
            ));

    // output
    map.forEach((k, v) -> System.out.println(k + "=" + v));
    //English=[Country:UK, Country:US]
    //French=[Country:France, Country:Canada]
}
static class Country {
    String name;
    List<String> languages;

    public Country(String name, List<String> languages) {
        this.name = name;
        this.languages = languages;
    }

    public List<String> getLanguages() {
        return languages;
    }

    @Override
    public String toString() {
        return "Country:" + name;
    }
}

答案 2 :(得分:0)

做到这一点:

    countries.stream()
             .flatMap(country -> country.getLanguages()
                                        .stream()
                                        .map(lang -> new SimpleEntry<>(lang, new ArrayList<>(Arrays.asList(country)))))
             .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (l1, l2) -> {
                 l1.addAll(l2);
                 return l2;
             }));