I have a list of Station, in each Station there is a list of radios. I need to create a lookup Map of radio to Station. I know how to use Java 8 stream forEach to do it:
If you are open to using a third-party library, there is the method groupByEach
from Eclipse Collections:
Multimap multimap =
Iterate.groupByEach(stationList, Station::getRadioList);
This can also be written using Java 8 Streams with the Collectors2
utility from Eclipse Collections:
Multimap multimap =
stationList.stream().collect(
Collectors2.groupByEach(
Station::getRadioList,
Multimaps.mutable.list::empty));
Note: I am a committer for Eclipse Collections.