Demonstrating similar behavior as filter an array list and filter map by key, this example will demonstrate how to check if every element in the array list satisfies a condition. In the set up method we will create a Camera class and seed a list of cameras in which the snippets of code will check if they have a medium telephoto lens with a focal length of 80mm and above.
Setup
Straight up Java
This snippet will show how to check if the array list contains all elements of a specified criteria using java. It will use a standard for loop and check if a camera has a focal length of less than 80 using java.
Java 8
This snippet will check if each element in a collection matches a specified condition using java 8. Using the Streams API introduced in Java 8, we will call Stream.allMatch, a terminal stream operation, passing in a java 8 predicate created by a lambda expression. This condition will check if a camera has a focal length greater than or equal to 80.
Google Guava
Guava Iterables.all will return true if every camera in the array list has a focal length greater than or equal to 80.
Apache Commons
Similar to guava and java predicate, apache commons has a predicate class as well. Below we will call CollectionsUtils.filter passing it a apache commons predicate that will return true if a camera has a focal length of greater than or equal to 80.