Monday, August 12, 2019

Java 8 IntStream allMatch() Method Example

1. Overview


In this article, We'll be learning a new java 8 API IntStream allMatch() method. This is very useful to match a condition to all of the IntStream values.

As we know, IntStream accepts only integer values (This is designed for only integer values to improve performance and avoid errors). Performance is enhanced by avoiding the type casting in case of String has only numbers.

API Note:

Returns whether all 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 true is returned and the predicate is not evaluated.

we have already seen a similar method allMatch() in Stream API.


2. allMatch() Method


Syntax:

boolean allMatch(IntPredicate predicate)


This method takes the IntPredicate argument and returns boolean.

IntPredicate is a Functional Interface which has only one abstract method test(int value) and this FI is only for int values validation.

allMatch() method returns true if all values are matched to the given IntPredicate. Returns false, if any of the value is not matched to the given predicate. We are going to write a few examples in both positive and negative cases.

3. allMatch() method returning true example


Taking now int values only even numbers such as 10, 20, 30, 40, 50, 60. We will write an IntPredicate to test a number is even.

package com.java.w3schools.blog.java8.stream.intstream;

import java.util.function.IntPredicate;
import java.util.stream.IntStream;

/**
 * Java 8 IntStream allMatch() Method Example
 * 
 * @author venkateshn
 *
 */
public class IntStreamAllMatchExample {

 public static void main(String[] args) {
  IntStream evenStream = IntStream.of(10, 20, 30, 40, 50, 60);
  IntPredicate intPredicate = value -> value % 2 == 0;

  boolean isAllEven = evenStream.allMatch(intPredicate);

  System.out.println("Are all values of Int Stream are even ? " + isAllEven);

 }

}

Output:

Are all values of Int Stream are even ? true

4. allMatch() method returning false example


Now, Writing a program to check all numbers are divisible by 5 or not.

package com.java.w3schools.blog.java8.stream.intstream;

import java.util.function.IntPredicate;
import java.util.stream.IntStream;

/**
 * Java 8 IntStream allMatch() Method Example
 * 
 * @author venkateshn
 *
 */
public class IntStreamAllMatchExample2 {

 public static void main(String[] args) {
  IntStream divisibleStream = IntStream.of(10, 20, 30, 40, 50, 60, 77);
  IntPredicate divisible5Predicate = value -> value % 5 == 0;

  boolean isAlldivisibleby5 = divisibleStream.allMatch(divisible5Predicate);

  System.out.println("All are values are divided by 5 : " + isAlldivisibleby5);

 }

}

Output:

All are values are divided by 5 : false

allMatch() method is returned false Because all are divisible by 5 except 77.

5. What happens if IntStream is empty


Now input IntStream is empty and it has no values. We will write a sample program and see the output.


package com.java.w3schools.blog.java8.stream.intstream;

import java.util.function.IntPredicate;
import java.util.stream.IntStream;

/**
 * Java 8 IntStream allMatch() Method Example
 * 
 * @author venkateshn
 *
 */
public class IntStreamAllMatchExample {

 public static void main(String[] args) {

  IntStream emptyStream = IntStream.empty();

  IntPredicate predicate = number -> number > 0;

  boolean emptyFlag = emptyStream.allMatch(predicate);
  System.out.println("Empty stream allmatch value : " + emptyFlag);

 }

}

Output:

Empty stream allmatch value : true


allMatch() method returned true. In this case, the passed predicate will be ignored and checks first stream is empty. If empty then returns true. In this case, an IntPredicate condition can be anything. It is implemented internally in such a passion.

5. Conclusion


In this article, We've covered how to check a condition against all values of IntStream. This method works similar to the Stream allMatch() Method.

And also seen its syntax and what it does. IntPredicate condition is mandatory to allMatch() method. If we pass IntPredicate as null then it throws java.lang.NullPointerException.

Implemented a few examples programs to test even numbers and a number is divisible by 5. Finally, We've seen what happens if int stream is empty.

All codes shown are over GitHub.
API Ref
Type Casting Ref

No comments:

Post a Comment