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

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -