How to remove a collect into a new stream from the middle of a java 8 stream? -
i'm working on java 8 stream. , need group 2 keys in map. , put these keys value new function.
is there way skip collector , reading out again?
graphs.stream() .map(abstractbasegraph::edgeset) .flatmap(collection::stream) .collect(collectors.groupingby( graph::getedgesource, collectors.groupingby( graph::getedgetarget, collectors.counting() ) )) .entryset().stream() .foreach(startentry -> startentry.getvalue().entryset().stream() .foreach(endentry -> graph.setedgeweight( graph.addedge(startentry.getkey(), endentry.getkey()), endentry.getvalue() / strains )));
no, have have sort of intermediate data structure accumulate counts. depending on how graph , edge classes written, try accumulate counts directly graph, less readable , more brittle.
note can iterate on intermediate map more concisely using map#foreach:
.foreach((source, targettocount) -> targettocount.foreach((target, count) -> graph.setedgeweight(graph.addedge(source, target), count/strains) ) ); you can collect counts map<list<node>, long> instead of map<node,map<node,long>> if dislike map-of-maps approach:
graphs.stream() .map(abstractbasegraph::edgeset) .flatmap(collection::stream) .collect(groupingby( edge -> arrays.aslist( graph.getedgesource(edge), graph.getedgetarget(edge) ), counting() )) .foreach((nodes, count) -> graph.setedgeweight(graph.addedge(nodes.get(0), nodes.get(1)), count/strains) );
Comments
Post a Comment