Java Stream How to - Use Predicate to check a condition








Question

We would like to know how to use Predicate to check a condition.

Answer

//from   ww w  .ja  v  a  2  s.c o m
import java.util.function.Predicate;

public class Main {
  public static void main(String... args) {
    Predicate<String> predicate = (s) -> s.length() > 0;

    predicate.test("foo");              // true
    predicate.negate().test("foo");     // false  
  }
}