Desmond Desmond - 6 months ago 35
Java Question

Properly terminate java stream

neighboursOfCurrentVertexList =
.filter(vertex -> !vertex.isMarked())
.map(vertex -> {
return vertex;

I have implemented this breadthFirstSearch algorithm using java streams. First, i filtered to check if the vertex is marked, then if it is not marked, i add it to the queue. When i am using the .map, i need to end off with a terminating operation like .collect(Collectors.toList()).

My question is that this does not look right to me because I am using collect which returns a new list of the filtered vertex. What terminal operation should i be using in this case? I have no need of collecting the new list. I just want to use the map operation that is all. Thank you

Answer Source

Use .forEach instead of .map:
        .filter(vertex -> !vertex.isMarked())
        .forEach(vertex -> {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download