Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Friday, May 29, 2020

Java 8 Stream allMatch() Method Example

1. Introduction


In this tutorial, We will see the example program on Java 8 Stream allMatch() method. allMatch() method checks whether all elements of Stream is matched to the given predicate then it returns true. Otherwise returns false.

Predicate is nothing but a condition.

This is called Terminal Operation because this is executed at the end of the stream. That means Stream is closed after the execution of allMatch() method.


Java 8 Stream allMatch() Method Example


Java 8 Stream allMatch()





2. Stream allMatch() Syntax

boolean allMatch​(Predicate<? super T> predicate)

Now, we will see the syntax.

boolean allMatch (Predicate<? super T> predicate)

Note: If the stream is empty then true is returned and the predicate is not evaluated.

Predicate is a built-in Functional Interface.

3. Stream allMatch() Example 1

import java.util.function.Predicate;
import java.util.stream.Stream;

public class AllMatchExample {

 public static void main(String[] args) {

  // Predicate to find the number are even.

  Stream stream = Stream.of(2, 4, 6, 8, 10);
  
  Predicate p1 = i -> i % 2 == 0;
  boolean isAllMatch = stream.allMatch(p1);
  System.out.println("isAllMatch for even numbers : " + isAllMatch);

  // Predicate to find the number are even.

  Stream oddStream = Stream.of(2, 4, 6, 8, 10);

  Predicate p2 = i -> i % 2 == 1;
  isAllMatch = oddStream.allMatch(p2);
  System.out.println("isAllMatch for odd numbers : " + isAllMatch);

 }
}

Output:
isAllMatch for even numbers : true
isAllMatch for odd numbers : false

4. Stream allMatch() Example 2

Creating Employee Class


public class Employee {

 private int id;
 private String name;

 public Employee(int id, String name) {
  this.id = id;
  this.name = name;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 // Overrding toString method.
 @Override
 public String toString() {
  return "Employee [id=" + id + ", name=" + name + "]";
 }

}


Usage of allMatch() method:

Employee employee1 = new Employee(1000, "Alan Joel");
Employee employee2 = new Employee(2000, "Jhon Millon");

List empList = new ArrayList<>();
empList.add(employee1);
empList.add(employee2);

Predicate nameLengthPrediacate = e -> e.getName().length() > 0;
boolean namelength = empList.stream().allMatch(nameLengthPrediacate);
System.out.println("namelength allmatch : " + namelength);

Predicate namePrediacate = e -> e.getName().contains("Jhon");
boolean nameMatch = empList.stream().allMatch(namePrediacate);
System.out.println("nameMatch allmatch : " + nameMatch);

Output:
namelength allmatch : true
nameMatch allmatch : false


5. Conclusion


In this article, you have seen how to check whether all values of list are matching to a particular condition using Stream allMatch() method.


No comments:

Post a Comment

Please do not add any spam links in the comments section.