Search code examples
javajava-streammatchingoperations

Java stream - purpose of having both anyMatch and noneMatch operations?


The anyMatch operation will return true if it finds an element - the noneMatch operation will return false it if finds a matching element.

The anyMatch operation will return false if it finds no matching element - the noneMatch operation will return true if finds no matching element.

Therefore, instead of having both of these operations, could we not just do with one, or am I missing something? In essence, anyMatch returning false is a way of evaluating the truth of noneMatch's predicate.


Solution

  • Same reason you have a != b, instead of only supporting ! (a == b):

    • Easy of use.
    • Clarity of purpose.