Stream API AllMatch in Java 8 with examples
In this blog post, I will be demonstrating the allMatch method provided by Java 8 Stream API.
In order to understand the Stream API in detail, refer to this blog post.
Table Of Contents
Code Sample with Integer
You can use the allMatch method to determine if all the elements in a Collection matches a particular condition.
Consider the following code snippet:
public class AllMatchIntegerDemo { public static void main(String[] args) { List<Integer> input = Arrays.asList(5, 3, 11, 15, 9, 2, 5, 11); boolean found = input.stream().allMatch(num -> num % 2 == 0); System.out.println("All even numbers: "+found); } }
This code checks if all the numbers in the input stream are even. First, the code obtains a Stream on the input ArrayList using the stream() method. Then the code invokes the allMatchmethod 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 ieven and if so returns a boolean value accordingly.
So when you execute this code, it will print the following output:
All even numbers: false
Code Sample with String
The following code demonstrates the allMatch method with a String ArrayList as input:
public class AllMatchStringDemo { public static void main(String[] args) { List<String> strList = Arrays.asList("Monday","Sunday","Wednesday"); boolean allMatch = strList.stream().allMatch( str -> str.endsWith("day")); System.out.println("All days of the week: "+allMatch); } }
Here, the allMatch method is used to determine if all the Strings in the input list are days of the week and end with “day“. So when you execute this code, it prints the following output:
All days of the week: true
Code Sample with Class
The following code demonstrates the allMatch method with an ArrayList of a custom type called Person.
public class Person { private String name; private int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } }
This code defines a class Person with two fields, “name” and “age“. Now consider the following code that creates a List of Person objects and uses the allMatch method:
public class AllMatchClassDemo { public static void main(String[] args) { List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom",25)); personList.add(new Person("Jane",31)); personList.add(new Person("Ram",15)); boolean adultPresent = personList.stream().allMatch(person -> person.getAge() > 18); System.out.println("All adults present="+adultPresent); } }
This code uses the allMatch method to determine if all the Person objects in the List are adults, that is they have age > 18. So when you execute this code, it will print the following output:
All adults present=false
You can get the source code for this example along with the code for other Java 8 examples at the Github repository here.