Java Stream allMatch() with examples

 517 total views,  3 views today

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

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

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

Method Description

boolean allMatch(Predicate<? super T> predicate)

Returns whether all elements of this stream match the provided predicate.

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

Java stream allMatch() operation example using lambda

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

        // Check if the element of a stream is greater than 3
        boolean result = list.stream().allMatch(i -> i > 3);
        System.out.println("allMatch found : " + result);

        // Check if the element of a stream is greater than 5
        result = list.stream().allMatch(i -> i > 5);
        System.out.println("allMatch found : " + result);
    }
}

Output

allMatch found : true
allMatch found : false

In this example, we are checking if every element is greater than 3.

In the next line, we are checking if every element is greater than 5.

Java stream allMatch() operation example using Predicate

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

Output

AnyMatch found : true

Java stream allMatch() 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 all elements is of length 1
        boolean result = listString
                           .stream()
                           .allMatch(i -> i.length() == 1);
        System.out.println("allMatch found : " + result);
    }
}

Output

allMatch found : true

Conclusion

allMatch() operation is a terminal operation and it is used to identify whether every element of the stream matches the given condition or predicate.

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!.