Java Stream How to - Create Predicate lambda and call test method








Question

We would like to know how to create Predicate lambda and call test method.

Answer

import java.util.function.Predicate;
//from   w  w w .ja va  2s  .  c om
public class Main {

  public static void main(String[] args) {
    Predicate<Integer> prime = (value) -> {
          if (value <= 2) { 
                return (value == 2); 
            } 
            for (long i = 2; i * i <= value; i++) { 
                if (value % i == 0) { 
                    return false; 
                } 
            } 
            return true; 
    };
    
    System.out.println("Primzahl 1: " + prime.test(1));
    System.out.println("Primzahl 1: " + prime.test(3));
  }

}

The code above generates the following result.