How to use AnyMatch in IntStream?

Here is a example to explain how to use AnyMatch(IntPredicate, predicate) in IntStream?

Syntax:

Modifier and Type         Method
boolean                   anyMatch(IntPredicate predicate)

 

package com.nourit.java8.Streams.IntStream;

import java.util.stream.IntStream;

public class IntStreamAnyMatch {

  public static void main(String[] args) {
    System.out.println("---------- First Example -----------------");
    IntStream i = IntStream.of(6, 5, 7, 0, 1, 2, 3, 4);
    //true if at least one of the numbers is greater than 0
    boolean d1 = i.anyMatch(n -> n > 0);
    System.out.println(d1);
    System.out.println("---------- Second Example -----------------");
    
    boolean d2 = IntStream.of(6, 3, 1, 5, 7).anyMatch(k -> k % 2 == 0);  
    System.out.println(d2);
    System.out.println("----------------------------------------");
  }
}

Output:

---------- First Example -----------------
true
---------- Second Example -----------------
true
----------------------------------------

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*