Java, search for an item, if not found, add it

I am new to streams.

I would like to pass an arraylist geometries EC_Geometry, and if the element is EC_Geometrymissing (or better equalsnever returns true), I add it.

public void init(GL3 gl3, EC_Mesh mesh) {

    geometries.stream()
            .filter(geometry -> mesh.getGeometry().equals(geometry))
            .findAny()
            .orElse(..?);
}

But I'm stuck on the last line

How can I solve this with threads?

Note that equalsthis is a method that I wrote, checking if the geometry is the same (i.e. if the triangles match)

+4
source share
2 answers

orElse , , orElseGet , , .

 geometries.stream()
            .filter(geometry -> mesh.getGeometry().equals(geometry))
            .findAny()
            .orElseGet(() -> {
                geometries.add(mesh.getGeometry());
                return mesh.getGeometry();
            });
+4
.findAny().orElse(..?);

- .

:

meshG = mesh.getGeometry();
if (!geometries.contains(meshG)) {
   geometries.add(meshG);
}

Stream API.

+2

All Articles