Check if all items in a list are set to the same boolean value

You can use Stream.noneMatch() to do this:

if (bricks.stream().noneMatch(GameObj::isVisible)) {
    doStuffIfAllBricksAreInvisible();
}

This returns true, if all bricks are invisible.

Additionally I would recommend to take a look at Stream.allMatch(), which returns true, if all elements of the list match the given predicate.

Using allMatch() this would look like this:

if (bricks.stream().allMatch(b -> !b.isVisible())) {
    doStuffIfAllBricksAreInvisible();
}

To complete this, you also can take a look at Stream.anyMatch(), which returns true, if one of the elements matches the given predicate.

Tags:

Java

Arraylist