Let's say I have a list of Foo objects. There is a property on Foo that I must use to fetch Bar objects from a data source. I must then map each bar object back to the original Foo object I got Bar for.
public class Foo {
int barId;
}
public class Bar {
int barId;
}
Set<Foo> inputFoo;
Map<Bar, Foo> barToFoo;
public Bar getBar(int barId);
My attempt is as follows:
List<Bar> allBarsInFoo = inputFoos.stream()
.map(Foo::barId)
.forEach(b -> getBar(b))
I do not know how to get the map of Bar to Foo without doing more heavy and unnecessary ops after the above.
Edit: To make my question more general, is there a way to keep a reference to the original object, perform a bunch of filter/map operations in a stream off the list of original objects, and then map the result of the filter/map as the key and the original object as the value of the resulting map?