Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79055

Why is the behaviour of anyMatch different from that of allMatch and noneMatch for an empty Stream?

While Stream#anyMatch returns false for an empty stream, Stream#allMatch and Stream#noneMatch return true for an empty stream.

Why is the behaviour of anyMatch different from that of allMatch and noneMatch for an empty Stream?

Upvotes: 2

Views: 905

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79055

Stream#anyMatch evaluates the existential quantification of the predicate over the elements of the stream. The existential quantification always evaluates to false for an empty set.

On the other hand, Stream#allMatch evaluates the universal quantification of the predicate over the elements of the stream and Stream#noneMatch evaluates the universal quantification of the negated predicate over the elements of the stream. The universal quantification always evaluates to true for an empty set.

Upvotes: 2

Related Questions