Java 8 convert List to Lookup Map -
i have list of station, in each station there list of radios. need create lookup map of radio station. know how use java 8 stream foreach it:
stationlist.stream().foreach(station -> { iterator<long> = station.getradiolist().iterator(); while (it.hasnext()) { radiotostationmap.put(it.next(), station); } }); but believe there should more concise way using collectors.mapping().
anyone can help?
this should work, , doesn't need third parties.
stationlist.stream() .map(s -> s.getradiolist().stream().collect(collectors.tomap(b -> b, b -> s))) .flatmap(map -> map.entryset().stream()) .collect(collectors.tomap(map.entry::getkey, map.entry::getvalue));
Comments
Post a Comment