Java 8 stream allMatch, noneMatch and anyMatch examples

In this post we will provide three examples on how to use the java 8 stream allMatch, noneMatch and anyMatch methods.

Java 8 stream allMatch, noneMatch and anyMatch examples:

Stream allMatch() Example:

The allMatch() method is used on a stream to determine, if all the values satisfy a certain condition(predicate). The example below, checks to see if all the values in a List of numbers is less then 50.

Running the above code produces the following:

Stream noneMatch() Example:

The noneMatch() method is the opposite of the allMatch() method. It checks to see if all the values do not satisfy a certain condition(predicate). The next example uses the noneMatch() method to determine if all the values in an integer list is less then 50.

Running the above examples produces the following:

Stream anyMatch() Example:

The anyMatch() method is used to determine if at least one element in a stream matches a certain condition. The final example checks that at least one element is equal to 50.

Running the final example produces the following:

I hope the above examples have helped you understand how the anyMatch, noneMatch and allMatch methods work.

Similar posts:

  1. List only contains certain values
  2. Streams map example
  3. Streams flatmap examples
  4. Java 8 reduce operation

References:

  1. Java Streams API

 

Leave a Reply