Why does Stream.allMatch() return true for an empty stream?

This is known as vacuous truth. All members of an empty collection satisfy your condition; after all, can you point to one that doesn’t?

Similarly, anyMatch returns false, because you can’t find an element of your collection that does match the condition. This is confusing to a lot of people, but it turns out to be the most useful and consistent way to define “any” and “all” for empty sets.

Leave a Comment