List contains any element

This java example will demonstrate how to check if any element in an arraylist satisfies a specified condition. In the @setUp method we will initialize an arraylist of Vehicles which we will use in the snippets below.

Setup

class Vehicle {

    String manufacturer;
    String model;
    int year;
    
    public Vehicle(String manufacturer, String model, int year) {
        super();
        this.manufacturer = manufacturer;
        this.model = model;
        this.year = year;
    }
    
    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .add("manufacturer", manufacturer)
                .add("model", model)
                .add("year", year)
                .toString();
    }
}

List<Vehicle> vehicles;

@Before
public void setUp () {
    vehicles = Lists.newArrayList();
    vehicles.add(new Vehicle("Dodge", "CHARGER", 1965));
    vehicles.add(new Vehicle("Dodge", "VIPER CONVERTIBLE", 2006));
    vehicles.add(new Vehicle("Oldsmobile", "BRAVADA", 2004));
    vehicles.add(new Vehicle("GMC", "C1500 YUKON 2WD HYBRID", 2012));
    vehicles.add(new Vehicle("GMC", "K1500 YUKON 4WD", 2013));
    vehicles.add(new Vehicle("Chrysler", "PT Cruiser", 2006));
}

Straight up Java

This snippet will show how to check if the collection contains any elements that match a specified criteria using java. It will use a java enhanced for loop and check if any of the vehicles in the list are five years or older. If the condition is met, we will break the loop after setting the vehiclesFiveYearOrOlder to true.

@Test
public void list_contains_any_java () {
    
    // assuming 2013
    boolean vehiclesFiveYearOrOlder = false;
    for (Vehicle vehicle : vehicles) {
        if ( (vehicle.year - 5) <= 2008) {
            vehiclesFiveYearOrOlder = true;
            break;
        }
    }

    assertTrue(vehiclesFiveYearOrOlder);
}

Java 8

This snippet will check if any element in an arraylist matches a specified condition using java 8. Using the Streams API introduced in Java 8, we will call Stream.anyMatch, a terminal operation, passing in a java predicate created by a lambda expression. This condition will check if a vehicle manufacturer equals "Dodge".

@Test
public void list_contains_any_java8_lambda() {

    boolean vehiclesContainDodge = vehicles.stream().anyMatch(
            p -> p.manufacturer.equals("Dodge"));

    assertTrue(vehiclesContainDodge);
}

Google Guava

Using Iterables, a guava collection utility, we will call Iterables.any passing a guava predicate which will return true if any vehicle manufacturer equals "Dodge".

@Test
public void list_contains_any_guava() {

    boolean vehiclesContainDodge = Iterables.any(vehicles,
            new Predicate<Vehicle>() {
                public boolean apply(Vehicle input) {
                    return input.manufacturer.equals("Dodge");
                }
            });

    assertTrue(vehiclesContainDodge);
}

Apache Commons

Similar to guava and java predicate, apache commons also has a predicate. Below we will call CollectionsUtils.exists passing it a apache commons predicate that will return true if at least one vehicle model contains "YUKON".

@Test
public void list_contains_any_apache_commons() {

    boolean vehiclesContainYukon = CollectionUtils.exists(vehicles,
            new org.apache.commons.collections.Predicate() {
                public boolean evaluate(Object object) {
                    Vehicle vehicle = (Vehicle) object;
                    return vehicle.model.contains("YUKON");
                }
            });

    assertTrue(vehiclesContainYukon);
}