Build a Set of Node.Leaf.Id in one line with Java 8 -



Build a Set of Node.Leaf.Id in one line with Java 8 -

i have 2 entities :

leaves nodes, contains leaves.

i have collection<node> , trying build set<integer> of leaf ids in 1 line of code. sense might possible streams until now, can dot :

set<integer> leafids = sets.newhashset(); root.getnodes() .foreach(node -> node.getleaves() .foreach(leaf -> leafids.add(leaf.getid())));

i don't part manually create collection , add together elements method collection.add() (not thread safe, unsafe , not optimized). sense might possible :

root.getnodes() .stream() .??? .getleaves() .map(leaf::getid) .distinct() .collect(collectors.toset());

any idea?

it possible. flatmap can stream<node> stream<leaf> of leaves of nodes :

set<integer> leaves = root.getnodes().stream() .flatmap (n -> n.getleaves().stream()) .distinct() .map(leaf::getid) .collect(collectors.toset());

java java-8

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -