Java 8 Examples

Stream API anyMatch explained with code samples

Posted On
Posted By admin

In this blog post, I will be demonstrating the anyMatch method provided by Java 8 Stream API.

In order to understand the Stream API in detail, refer to this blog post.

Code Sample with Integer

You can use the anyMatch method to determine if any element in a Collection matches a particular condition.

Consider the following code snippet:

public class AnyMatchDemo {
  public static void main(String[] args) {
    List<Integer> input = Arrays.asList(5, 3, 11, 15, 9, 2, 5, 11);
    boolean found = input.stream().anyMatch(num -> num > 9);
    System.out.println("number > 9 "+found);
  }
}

This code checks if there is any number in the input collection that is greater than 9. First, the code obtains a Stream on the input ArrayList using the stream() method. Then the code invokes the anyMatch method on the stream instance. This method accepts a Predicate instance. Predicate is an in-built functional interface.  Refer this blog post for a detailed Predicate example. It has a method called test. This method accepts an argument of any data type and returns a boolean. The above code implements the Predicate via a lambda expression. So the code passes each element in the input list to the Predicate, checks if the number is greater than 9 and if so returns a boolean value accordingly.

So when you execute this code, it will print the following output:

number > 9 true

Code Sample with String

The following code demonstrates the anyMatch method with a String ArrayList as input:

public static void main(String[] args) {
  List<String> strList = Arrays.asList("Apple","Orange","Mango","Banana");
  boolean anyMatch = strList.stream().anyMatch( str -> str.startsWith("S"));
  System.out.println("Fruit starting with S present: "+anyMatch);

}

Here, the anyMatch method is used to determine if there is any fruit in the input list that starts with an “S“.  So when you execute this code, it prints the following output:

Fruit starting with S present: false

Code Sample with Class

The following code demonstrates the anyMatch method with an ArrayList of a custom type called Animal.

public class Animal {
  
  private String name;
  
  private String type;

  public Animal(String name, String type) {
    super();
    this.name = name;
    this.type = type;
  }
        //getters and setters

}

This code defines a class Animal with two fields, “name” and “type“. The name signifies the name of the Animal and type signifies whether the Animal is a herbivore or carnivore. Now consider the following code that creates a List of Animals and uses the anyMatch method:

public static void main(String[] args) {
    List<Animal> animals = new ArrayList<Animal>(); 
    animals.add(new Animal("cow","herbivore"));
    animals.add(new Animal("lion","carnivore"));
    animals.add(new Animal("tiger","carnivore"));
    animals.add(new Animal("giraffe","herbivore"));
    animals.add(new Animal("zebra","herbivore"));
    
    boolean herbivorePresent = animals.stream().anyMatch(animal -> animal.getType().equals("herbivore"));
    System.out.println("Herbivore present="+herbivorePresent);

  }

This code uses the anyMatch method to determine if there is any Animal in the List that is a Herbivore. So when you execute this code, it will print the following output:

Herbivore present=true

You can get the source code for this example along with the code for other Java 8 examples at the Github repository here.

If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my new course Learn Java 8 New Features

If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

One thought on “Stream API anyMatch explained with code samples
  1. Stream API AllMatch Example - LearnJava

    […] Stream API anyMatch example […]

leave a Comment