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:
We can save the intermediate step of collectiong to a Map by transforming directly to a Stream of SimpleEntry, for example:
Map result = stationList.stream()
.flatMap(station -> station.getRadioList().stream().map(radio -> new SimpleEntry<>(radio, station)))
.collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue));