4

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?

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • No, you have it backwards. A `Map` would imply a one-to-one mapping between `Bar` and `Foo` instances. Consider `Map` or `Map`, where a person can have multiple credit cards and a book can have multiple ISBNs. Nothing complicated here. – Garret Wilson Jun 23 '16 at 22:33
  • 1
    I do understand the meaning, but I didn't want to get hung up on points that are a bit ancillary. Whether a mapping is bidirectional depends on whether the graph is directional. A Java `Map` is a unidirectional, so you can't map back---which is why Guava has a `BiMap`, for instance. I think you wanted to say that the _relationship_ is one-to-one---in this case the _relationship_ is many-to-one (`Bar`-`Foo`). But again this is all sort of beside the point---things to debate over a beer. The summary is that each `Foo` can have many `Bar`, and I want to map `Bar` to `Foo`. Cheers! – Garret Wilson Jun 24 '16 at 15:59
  • Indeed, we can close with the statement that each `Foo` can have many `Bar` and that all `Bar`s are distinct and, well, I think [Sotirios’ answer](http://stackoverflow.com/a/37956805/2711488) contains an appropriate solution. Is there anything that stops you from accepting his answer, that we should address? – Holger Jun 24 '16 at 16:05
  • I was hoping for something a little more elegant and concise; without explicit intermediaries. I'll hold out for a little longer and see if anyone comes up with any tricks. – Garret Wilson Jun 24 '16 at 16:14
  • I tried with a specialized collector, then I realized that this not only is less readable, in the end it only replaces the explicit instantiation of `Map.Entry` instances with implicit instantiation of capturing lambda instances, so there is no improvement at all. – Holger Jun 24 '16 at 17:36

2 Answers2

9

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.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 1
    The `SimpleEntry<>` use is cool---I hadn't realized Java had something at least similar to a `Pair`. Overall though I was hoping for something a little more compact, but this may be the best we can get. I'll leave it for a while longer to see if we get any other options. – Garret Wilson Jun 24 '16 at 16:16
  • 5
    Java 9 will add the possibility to use `Map.entry(bar, foo)` to construct value-based, immutable entries in a more concise way. – Holger Jun 24 '16 at 17:39
1

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));
dsboger
  • 496
  • 4
  • 8