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?