Monday, August 12, 2019

Java 8 IntStream anyMatch() Method Example

1. Overview


In this example tutorial, We'll be learning Java 8 IntStream API anyMatch() method to verify Predicate condition matches to any values in the IntStream.

In the last tutorial, We've seen how to check whether all elements are matched to predicate condition using allmatch() method.

Let us see anyMatch() syntax, Example programs on this method.

API Note:

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

2. anyMatch() Method


Syntax:

boolean anyMatch(IntPredicate predicate)

If this method finds any(at least) one match to the predicate then it returns true else false.

IntPredicate takes only int value and holds a condition.

IntPredicate agePredicate = age -> age > 18;

This int predicate checks if age is greater than 18 then returns true else false.

Note: This is a short-circuiting terminal operation.

Example:

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

import java.util.stream.IntStream;

/**
 * 
 * Java 8 IntStream anyMatch() Method Example
 * 
 * @author venkateshn
 *
 */
public class IntStreamAnyMatchExample {

 public static void main(String[] args) {

  IntStream intStream = IntStream.iterate(1, i -> i + 1).limit(10);
  boolean isAnyMatch = intStream.anyMatch(value -> value > 5);
  System.out.println(" Any value is matched to the given predicate conditon ? " + isAnyMatch);

 }

}

Output:

Any value is matched to the given predicate conditon ? true

Stream has values from 1 to 10 and the condition is value > 5. The condition is failed for the values from 1 to 5 and success for values from 6 to 10. So some values are matched to the condition hence returned true.

3. Example to return false


The above example program is returning true. We'll now see the example to return false. That means the predicate condition should not match any value in the stream.

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

import java.util.stream.IntStream;

/**
 * 
 * Java 8 IntStream anyMatch() Method false Example
 * 
 * @author venkateshn
 *
 */
public class IntStreamAnyMatchExample {

 public static void main(String[] args) {

  IntStream intStream = IntStream.iterate(100, i -> i + 1).limit(10);
  boolean isAnyMatch = intStream.anyMatch(value -> value > 500);
  System.out.println(
    "Predicate to value is greater than 500. Is any value is matched to this predicate :  " + isAnyMatch);

 }

}

Output:

Predicate to value is greater than 500. Is any value is matched to this predicate :  false

4. If IntStream is empty

If the input stream is empty and if we call anyMatch() what it returns.

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

import java.util.stream.IntStream;

/**
 * 
 * Java 8 IntStream anyMatch() Method false Example
 * 
 * @author venkateshn
 *
 */
public class IntStreamAnyMatchExample {

 public static void main(String[] args) {

  IntStream intStream = IntStream.iterate(100, i -> i + 1).limit(10);
  boolean isAnyMatch = intStream.anyMatch(value -> value > 500);
  System.out.println(
    "Predicate to value is greater than 500. Is any value is matched to this predicate :  " + isAnyMatch);

  // IntStream is empty
  IntStream emptyStream = IntStream.empty();
  boolean isDivisibleBy4 = emptyStream.anyMatch(value -> value % 4 == 0);
  System.out.println("isDivisibleBy4 for empty stream : " + isDivisibleBy4);
 }

}

Output:

isDivisibleBy4 for empty stream : false


anyMatch() method returns false because it has no value to check.

5. Conclusion


In this short article, We've covered the method anyMatch() from IntStream API.

Shown the examples when it returns true and false. And also what happens if the stream is empty.

All the examples code is over GitHub.
API ref



No comments:

Post a Comment