Garret Wilson
Garret Wilson

Reputation: 21316

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?

Upvotes: 5

Views: 17183

Answers (2)

dsboger
dsboger

Reputation: 496

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

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279890

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.

Upvotes: 9

Related Questions