I'm traversing a graph by its edges and want to have a list of all sources and targets. This is what I have so far:
public Set<Vertex> getVertices(){
Set<Vertex> vertices = this.edges.stream().map(edge -> edge.getSource()).collect(Collectors.toSet());
vertices.addAll(this.edges.stream().map(edge -> edge.getTarget()).collect(Collectors.toSet()));
return vertices;
}
Is there any way to get both source and target in the same mapping/collection step? Something like (PSEUDO-CODE):
edges.stream().collect(edge.getSource()).collect(edge.getTarget())
Or plain old Java 7
for ( Edge e : edges ){
vertices.add(e.getSource());
vertices.add(e.getTarget());
}
Cheers, Daniel
Set<Vertex> vertices = edges.stream()
.flatMap(e -> Stream.of(e.getSource(), e.getTarget()))
.collect(Collectors.toSet());
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments