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.

1 2 3 4 5 6 7 |
List<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(21); numbers.add(20); boolean allMatch = numbers.stream().allMatch(x -> x < 50); System.out.println("All values less then 50: " + allMatch); |

Running the above code produces the following:

1 |
All values less then 50: true |

#### 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.

1 2 3 4 5 6 7 8 |
List<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(21); numbers.add(50); numbers.add(20); boolean anyMatch = numbers.stream().noneMatch(x -> x > 50); System.out.println("List has no entry greater then 50: " + anyMatch); |

Running the above examples produces the following:

1 |
List has no entry greater then 50: true |

#### 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.

1 2 3 4 5 6 7 8 |
List<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(21); numbers.add(50); numbers.add(20); boolean anyMatch = numbers.stream().anyMatch(x -> x == 50); System.out.println("List has entry equal to 50: " + anyMatch); |

Running the final example produces the following:

1 |
List has entry equal to 50: true |

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

#### Similar posts:

- List only contains certain values
- Streams map example
- Streams flatmap examples
- Java 8 reduce operation

#### References: