IntStream allMatch() method in Java

Java 8Object Oriented ProgrammingProgramming

The allMatch() method of the IntStream class in Java returns whether all elements of this stream match the provided predicate.

The syntax is as follows

boolean allMatch(IntPredicate predicate)

Here, the predicate parameter is a stateless predicate to apply to elements of this stream. The IntPredicate represents a predicate of one int-valued argument.

The allMatch() method returns true if either all elements of the stream match the provided predicate or the stream is empty.

The following is an example to implement IntStream allMatch() method in Java

Example

 Live Demo

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(55, 65, 70, 90, 100);
      boolean res = intStream.allMatch(a -> a > 50);
      System.out.println("Does all the elements of the stream matches the predicate?"+res);
   }
}

Output

Does all the elements of the stream matches the predicate?true

Example

 Live Demo

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(15, 20, 25, 40, 50, 80);
      boolean res = intStream.allMatch(a -> a < 30);
      System.out.println("Do all the elements of the stream match the predicate? "+res);
   }
}

Output

Do all the elements of the stream match the predicate? False
raja
Updated on 30-Jul-2019 22:30:25

Advertisements