I am trying to convert Map<String, NavigableMap<Long, Collection<String>>> into List<String> Java 8.
I wrote some code but got stuck some where in mid.
userTopics.values().stream().map(
new Function<NavigableMap<Long, Collection<String>>, Collection<String>>() {
@Override
public Collection<String> apply(NavigableMap<Long, Collection<String>> t) {
return null; //TODO
}
}
);
Just flatMap that s**t:
List<String> values = nestedMap.entrySet()
.stream()
.map(Map.Entry::getValue)
.flatMap(m -> m.entrySet().stream())
.map(Map.Entry::getValue)
.flatMap(Collection::stream)
.collect(toList());
As Holger points out, this is neater:
List<String> values = nestedMap.values()
.stream()
.flatMap(m -> m.values().stream())
.flatMap(Collection::stream)
.collect(toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With