Java : Predicate with Examples
Predicate (Java SE 19 & JDK 19) API Examples.
You will find code examples on most Predicate methods.
Summary
final var predicate = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.equals(s.toUpperCase());
}
};
final var stream = Stream.of("aaa", "BBB", "ccc", "DDD");
final var ret = stream.filter(predicate).toList();
System.out.println(ret); // [BBB, DDD]
// An example with a lambda expression.
final var stream = Stream.of("aaa", "BBB", "ccc", "DDD");
final var ret = stream.filter(s -> s.equals(s.toUpperCase())).toList();
System.out.println(ret); // [BBB, DDD]
Methods
default Predicate<T> and (Predicate<? super T> other)
final var predicate1 = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.equals(s.toUpperCase());
}
};
System.out.println(predicate1.test("abcd")); // false
System.out.println(predicate1.test("ABCD")); // true
System.out.println(predicate1.test("XYZ")); // true
final var predicate2 = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.equalsIgnoreCase("abcd");
}
};
System.out.println(predicate2.test("abcd")); // true
System.out.println(predicate2.test("ABCD")); // true
System.out.println(predicate2.test("XYZ")); // false
final var and = predicate1.and(predicate2);
System.out.println(and.test("abcd")); // false
System.out.println(and.test("ABCD")); // true
System.out.println(and.test("XYZ")); // false
static <T> Predicate<T> isEqual (Object targetRef)
final var predicate = Predicate.<String>isEqual("abcd");
System.out.println(predicate.test("abcd")); // true
System.out.println(predicate.test("xyz")); // false
final var predicate = Predicate.<Integer>isEqual(123);
System.out.println(predicate.test(123)); // true
System.out.println(predicate.test(456)); // false
default Predicate<T> negate ()
final var predicate = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.equals(s.toUpperCase());
}
};
System.out.println(predicate.test("ABCD")); // true
System.out.println(predicate.test("xyz")); // false
final var negate = predicate.negate();
System.out.println(negate.test("ABCD")); // false
System.out.println(negate.test("xyz")); // true
static <T> Predicate<T> not (Predicate<? super T> target)
final var predicate = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.equals(s.toUpperCase());
}
};
System.out.println(predicate.test("ABCD")); // true
System.out.println(predicate.test("xyz")); // false
final var not = Predicate.not(predicate);
System.out.println(not.test("ABCD")); // false
System.out.println(not.test("xyz")); // true
default Predicate<T> or (Predicate<? super T> other)
final var predicate1 = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.equals(s.toUpperCase());
}
};
System.out.println(predicate1.test("abcd")); // false
System.out.println(predicate1.test("ABCD")); // true
System.out.println(predicate1.test("XYZ")); // true
final var predicate2 = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.equalsIgnoreCase("abcd");
}
};
System.out.println(predicate2.test("abcd")); // true
System.out.println(predicate2.test("ABCD")); // true
System.out.println(predicate2.test("XYZ")); // false
final var or = predicate1.or(predicate2);
System.out.println(or.test("abcd")); // true
System.out.println(or.test("ABCD")); // true
System.out.println(or.test("XYZ")); // true
boolean test (T t)
final var predicate = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.equals(s.toUpperCase());
}
};
System.out.println(predicate.test("ABCD")); // true
System.out.println(predicate.test("xyz")); // false
// An example with a lambda expression.
final var stream = Stream.of("aaa", "BBB", "ccc", "DDD");
final var ret = stream.filter(s -> s.equals(s.toUpperCase())).toList();
System.out.println(ret); // [BBB, DDD]