Predicate and Composition
Compose boolean tests.
Predicate and Composition is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Predicate Does
Predicate<T> takes a value and returns a boolean.
- Its method is
boolean test(T t). - It models a yes/no condition.
import java.util.function.Predicate;
public class Main {
public static void main(String[] args) {
Predicate<Integer> isEven = n -> n % 2 == 0;
System.out.println(isEven.test(4));
System.out.println(isEven.test(5));
}
}Predicate in filter
Stream.filter takes a Predicate and keeps only matching elements.
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Integer> nums = List.of(1, 2, 3, 4, 5, 6);
List<Integer> evens = nums.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println(evens);
}
}Combining with and
and builds a predicate that is true only when both predicates pass.
import java.util.function.Predicate;
public class Main {
public static void main(String[] args) {
Predicate<Integer> positive = n -> n > 0;
Predicate<Integer> even = n -> n % 2 == 0;
Predicate<Integer> both = positive.and(even);
System.out.println(both.test(4));
System.out.println(both.test(-2));
}
}Combining with or
or returns true when at least one predicate passes.
import java.util.function.Predicate;
public class Main {
public static void main(String[] args) {
Predicate<String> empty = String::isEmpty;
Predicate<String> shortStr = s -> s.length() < 3;
Predicate<String> either = empty.or(shortStr);
System.out.println(either.test(""));
System.out.println(either.test("hi"));
System.out.println(either.test("hello"));
}
}Negating with negate
negate flips a predicate's result, giving you the logical NOT.
import java.util.function.Predicate;
public class Main {
public static void main(String[] args) {
Predicate<Integer> even = n -> n % 2 == 0;
Predicate<Integer> odd = even.negate();
System.out.println(odd.test(3));
}
}Predicate.not Static Helper
Predicate.not(p) is a readable static way to negate, especially with method references.
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> words = List.of("", "a", "", "bc");
List<String> nonEmpty = words.stream()
.filter(Predicate.not(String::isEmpty))
.collect(Collectors.toList());
System.out.println(nonEmpty);
}
}Predicate.isEqual
Predicate.isEqual(target) builds a predicate testing equality with a fixed value using Objects.equals.
import java.util.function.Predicate;
public class Main {
public static void main(String[] args) {
Predicate<String> isJava = Predicate.isEqual("java");
System.out.println(isJava.test("java"));
System.out.println(isJava.test("python"));
}
}Chaining Multiple Conditions
You can chain and, or, and negate to express rich rules. Use parentheses to control grouping.
import java.util.function.Predicate;
public class Main {
public static void main(String[] args) {
Predicate<Integer> big = n -> n > 100;
Predicate<Integer> even = n -> n % 2 == 0;
Predicate<Integer> rule = big.and(even).or(n -> n == 0);
System.out.println(rule.test(0));
System.out.println(rule.test(102));
System.out.println(rule.test(101));
}
}BiPredicate for Two Inputs
BiPredicate<T, U> tests two arguments and also supports and, or, and negate.
import java.util.function.BiPredicate;
public class Main {
public static void main(String[] args) {
BiPredicate<String, Integer> hasLength = (s, n) -> s.length() == n;
System.out.println(hasLength.test("hello", 5));
}
}Predicates in anyMatch
Stream terminal operations like anyMatch, allMatch, and noneMatch all take a Predicate.
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> nums = List.of(1, 3, 5, 8);
System.out.println(nums.stream().anyMatch(n -> n % 2 == 0));
System.out.println(nums.stream().allMatch(n -> n > 0));
}
}Reusable Predicate Constants
Defining predicates as named variables makes filtering pipelines self-documenting and reusable.
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Predicate<String> nonBlank = s -> !s.isBlank();
List<String> input = List.of("a", " ", "", "b");
System.out.println(input.stream().filter(nonBlank).collect(Collectors.toList()));
}
}Quick Check
What does p.and(q) produce?
Recap
You composed boolean tests:
Predicate<T>usestestto return a boolean.and,or, andnegatecombine conditions with short-circuiting.Predicate.notandPredicate.isEqualare handy static helpers.BiPredicatetests two inputs.
Frequently asked questions
Is the “Predicate and Composition” lesson free?
Yes — the full text of “Predicate and Composition” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Predicate and Composition”?
Compose boolean tests. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Predicate and Composition” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Function and BiFunction
- Supplier and Consumer
- Predicate and Composition
- Custom Functional Interfaces