简体   繁体   中英

Java stream collect to map with multiple keys

I thought I was getting pretty good at Java 8 streams, but then...

I have a Foo interface:

public interface Foo {
  String getKey();
  Stream<Bar> bars();
}

I know I can collect a Stream<Foo> into a Map<String, Foo> using the key of each:

Map<String, Foo> foosByKey = fooStream.collect(
    Collectors.toMap(Foo::getKey, Function.identity()));

But what if I want to collect them into a Map<Bar, Foo> ? In other words, for each Foo in the steam, I want to put that Foo in the map keyed to every one of the Bar instances returned by Foo.bars() . Where do I start?

As suggested here , you'll want extract the Bar values from each Foo and create pairs of them. Once you have the pairs, you can collect them into a Map . For example,

Map<Bar, Foo> map = fooStream.flatMap(foo -> foo.bars().map(bar -> new SimpleEntry<>(bar, foo)))
            .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); 

We use SimpleEntry here because it's available (Java doesn't have a simpler Pair type). You could write your own to be more specific.

You could define a new collector for that. One simple implementation (that always creates HashMap's of ArrayList's; no downstream support) could be:

public static <T, K>
Collector<T, ?, Map<K, List<T>>> multiGroupingBy(
        Function<? super T, Collection<? extends K>> multiClassifier) {
    return Collector.of(
            HashMap::new,
            (map, entry) -> {
                multiClassifier.apply(entry)
                        .forEach(
                                key -> map
                                        .computeIfAbsent(key,
                                                __ -> new ArrayList<>())
                                        .add(entry));
            },
            (map1, map2) -> {
                map2.forEach(
                        (key, list) -> map1
                                .computeIfAbsent(key,
                                        __ -> new ArrayList<>())
                                .addAll(list));
                return map1;
            });
}

Then you could call:

fooStream.collect(multiGroupingBy(Foo::bars));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM