Java Stream anyMatch() with examples

 333 total views,  1 views today

Welcome to Java 8 series. In this tutorial, you will learn about java stream anyMatch() with examples.

anyMatch() is a part of the Stream pipeline and terminal operation. It belongs to the Stream interface.

  • anyMatch() allows us to identify whether any element of the stream matches the given condition or predicate.
  • anyMatch() returns a boolean.
  • Once anyMatch() is applied to a stream, it returns a boolean result and the stream will be closed.
  • anyMatch() is a terminal operation and hence a stream cannot be reused once this function is applied.
  • anyMatch() returns false, if a stream is empty

Method Description

boolean anyMatch(Predicate<? super T> predicate)

Returns whether any elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then false is returned and the predicate is not evaluated. 

Let us now see a few examples of Java 8 stream anyMatch()

Java stream anyMatch() operation example using lambda

public class StaticReference
{
    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(3);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        
        //Check if the element of a stream has 3
        boolean result = list.stream().anyMatch(i -> i == 3);
        System.out.println("AnyMatch found : " + result);
    }
}

In this example, we are checking if the element of a stream has 3

Output

AnyMatch found : true

Java stream anyMatch() operation example using Predicate

public class StaticReference
{
    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(3);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);

        // Check if the element of a stream has 3
        Predicate<Integer> predicate = (i) -> i == 3;
        boolean result = list.stream().anyMatch(predicate);
        System.out.println("AnyMatch found : " + result);
    }
}

Output

AnyMatch found : true

Java stream anyMatch() operation example – String

public class StaticReference
{
    public static void main(String[] args)
    {
        List<String> listString = new ArrayList<>();
        listString.add("q");
        listString.add("b");
        listString.add("c");
        listString.add("d");

        // Check if the element of a stream has 3
        boolean result = listString
                           .stream()
                           .anyMatch(i -> i.equalsIgnoreCase("c"));
        System.out.println("AnyMatch found : " + result);
    }
}

Output

AnyMatch found : true

Conclusion

anyMatch() operation is a terminal operation and it is used to identify whether any element of the stream matches the given condition or predicate. anyMatch() returns false if the stream is empty.

References : Official documentation

Thanks for reading. Please a comment and give us a thumbs up! 

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.