Reputation: 381
I don't understand how to use the results of filtering a Stream<>. Example:
Stream<Edge> edgesStr = graph.edges().filter(edge -> edge.getNode1() == graph.getNode(ip2));
The above successfully(?) filters the Edge stream, keeping only those edges where the getNode() methods are equal. Now after this, I want to use those results, something like:
//for each result
Node node = edgeStr.getNode(); //method getNode() exists for objects Edge
foo1(Node);
foo2(Node);
Note: graph.edges() returns a Stream<Edges>
.
Upvotes: 1
Views: 213
Reputation: 2215
You can collect
the results via some of the Collector
methods, i.e. toList
when collecting into a List
. In order to extract the Node
out of the Edge
you can use map
.
List<Node> edges = graph.edges()
.filter(edge -> edge.getNode1() == graph.getNode(ip2))
.map(edge -> edge.getNode())
.collect(Collectors.toList());
Upvotes: 3
Reputation: 32036
If you need to perform void operations foo1
and foo2
on a Node
element, for each edge, you can do it as:
graph.edges()
.filter(edge -> edge.getNode() == graph.getNode(ip2))
.map(edge -> edge.getNode()) // map edge to corresponding Node
.forEach(node -> { foo1(node); foo2(node);}); // calls foo1 and foo2 on each node
Upvotes: 2