Edwin
Edwin

Reputation: 33

Java streams: groupingBy and flatMapping keys

Say I have a list of country objects that contain lists of languages spoken in that country, like so:

class Country {
    List<String> languages; 
}

I would like to create a map in the following format: Map<String, List<Country>>, such that a each language maps to a list of Country objects. E.g.:

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

Performance is an issue here, so I would like to avoid multiple iterations and lookups. I have tried using groupingBy, but how do I flatMap the keyset?

Example, this results in Map<List<String>, List<Country>>:

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

Upvotes: 1

Views: 776

Answers (3)

Not a JD
Not a JD

Reputation: 1902

This'll do it:

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;
                }));

Upvotes: 0

user14940971
user14940971

Reputation:

You can do it using a stream in a stream as follows: first iterate over the list of countries, then iterate over the nested list of languages and prepare the «language, country» pairs, and then collect them to map:

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

Upvotes: 2

Andreas
Andreas

Reputation: 159096

Since you seem to care about performance, don't use streams for this simple task:

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

Upvotes: 1

Related Questions