List contains all elements

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

class Camera {

    String name;
    int focalLength;

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .add("name", name)
                .add("focallength", focalLength)
                .toString();
    }

    public Camera(String name, int focalLength) {
        super();
        this.name = name;
        this.focalLength = focalLength;
    }
}

List<Camera> cameras;

@Before
public void setUp () {
    cameras = Lists.newArrayList();

    cameras.add(new Camera("Nikon", 80));
    cameras.add(new Camera("Cannon", 135));
    cameras.add(new Camera("Kodak", 300));
    cameras.add(new Camera("Olympus", 200));
    cameras.add(new Camera("Fujifilm", 600));
}

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.

@Test
public void list_contains_all_java () {

    boolean allCamerasOfMediumTelephoto = true;

    for (Camera camera : cameras) {
        if (camera.focalLength < 80) {
            allCamerasOfMediumTelephoto = false;
            break;
        }
    }

    assertTrue(allCamerasOfMediumTelephoto);
}

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.

@Test
public void list_contains_all_java8_lambda () {

    boolean allCamerasOfMediumTelephoto =
            cameras.stream().allMatch(p -> p.focalLength >= 80);

    assertTrue(allCamerasOfMediumTelephoto);
}

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.

@Test
public void list_contains_all_guava () {

    boolean allCamerasOfMediumTelephoto = Iterables.all(cameras, new Predicate<Camera>() {
        public boolean apply(Camera input) {
            return input.focalLength >= 80;
        }
    });

    assertTrue(allCamerasOfMediumTelephoto);
}

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.

@Test
public void list_contains_all_apache () {

    @SuppressWarnings("unchecked")
    Collection<Camera> mediumTelephotoCameras = CollectionUtils.select(cameras,
            new org.apache.commons.collections.Predicate() {
        public boolean evaluate(Object object) {
            Camera camera = (Camera) object;
            return camera.focalLength >= 80;
        }
    });

    assertTrue(mediumTelephotoCameras.size() == cameras.size());
}