Convert Map > > to list using java 8
•
Java
I'm trying to convert map < string, navigablemap < long, collection < string > > into list < string > java 8
I wrote some code, but I got stuck in the middle
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
}
}
);
Solution
Flatmap that s * * t only:
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 cleaner:
List<String> values = nestedMap.values()
.stream()
.flatMap(m -> m.values().stream())
.flatMap(Collection::stream)
.collect(toList());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
