You’re now narrowing down to specific functional interfaces. Good—that’s where interviews actually test you. But don’t just memorize Predicate; understand how it behaves under pressure (composition, chaining, edge cases).
1. 📖 Definition¶
Predicate<T> is a built-in functional interface in java.util.function that takes one input and returns a boolean result.
It is used to test a condition.
boolean test(T t);
Interview-ready answer: “Predicate is a functional interface that takes one input and returns a boolean, commonly used for condition checking in Java.”
2. ❓ Why it is used¶
Without Predicate, you’d write separate methods or classes for every condition.
It allows:
- Passing conditions as arguments
- Reusing logic
- Cleaner filtering in collections
Interview-ready answer: “Predicate is used to represent conditions as reusable logic, especially for filtering and validation operations.”
3. 🕰️ Background / Need¶
Before Java 8:
- No standard way to pass conditions
- Used loops + if statements
- Logic tightly coupled with iteration
Example:
for (Integer n : list) {
if (n % 2 == 0) {
System.out.println(n);
}
}
Problems:
- Repetitive code
- No abstraction for condition
Java 8 introduced:
- Functional interfaces like
Predicate - Streams API for cleaner data processing
Interview-ready answer: “Before Java 8, conditions were hardcoded in loops. Predicate was introduced to abstract and reuse condition logic, especially in streams.”
4. 🧠 Core Idea / Working¶
Key Method:¶
boolean test(T t);
How it works:¶
- Lambda implements
test() - Used in APIs like
.filter()
Example:¶
Predicate<Integer> isEven = x -> x % 2 == 0;
Composition (critical for interviews):¶
Predicate<Integer> greaterThan10 = x -> x > 10;
Predicate<Integer> combined =
isEven.and(greaterThan10); // both conditions
Other methods:
and()or()negate()
Interview-ready answer: “Predicate works by implementing the test method using a lambda, and it supports chaining using and, or, and negate methods.”
5. 🧩 Variants / Types¶
1. Basic Predicate¶
Predicate<Integer> p = x -> x > 5;
2. Chained Predicate¶
p1.and(p2)
p1.or(p2)
p1.negate()
3. BiPredicate (two inputs)¶
BiPredicate<Integer, Integer> bp = (a, b) -> a > b;
4. Method Reference Predicate¶
Predicate<String> isEmpty = String::isEmpty;
Interview-ready answer: “Predicate can be used alone, chained using logical operations, or extended to BiPredicate for handling two inputs.”
6. ⚖️ Advantages¶
- Reusable condition logic
- Cleaner filtering with streams
- Supports chaining (AND/OR logic)
- Reduces boilerplate code
Interview-ready answer: “Predicate simplifies condition handling, improves code reuse, and integrates well with streams for filtering operations.”
7. ⚠️ Limitations / Disadvantages¶
- Only returns boolean (limited scope)
- Over-chaining reduces readability
- Debugging complex predicates is hard
When NOT to use:
- Complex multi-step business rules
- Conditions requiring multiple outputs
Interview-ready answer: “Predicate is limited to boolean results and can reduce readability if conditions become too complex or heavily chained.”
8. 💻 Code Examples¶
Example 1: Basic Predicate¶
import java.util.function.Predicate;
Predicate<Integer> isEven = x -> x % 2 == 0;
System.out.println(isEven.test(4)); // true
Example 2: Predicate with Stream¶
import java.util.*;
List<Integer> list = Arrays.asList(1, 2, 3, 4);
Predicate<Integer> isEven = x -> x % 2 == 0;
list.stream()
.filter(isEven) // filters even numbers
.forEach(System.out::println);
Example 3: Chaining Predicates¶
Predicate<Integer> greaterThan10 = x -> x > 10;
Predicate<Integer> isEven = x -> x % 2 == 0;
Predicate<Integer> combined =
greaterThan10.and(isEven);
System.out.println(combined.test(12)); // true
How to explain this in interview: “I used Predicate to define reusable conditions and applied it in stream filtering. I also used chaining methods like and() to combine conditions.”
9. 🌍 Real-World Use Cases¶
- Filtering collections (most common)
- Input validation
- Business rule checks
- Security filters (access conditions)
- Stream pipelines
Interview-ready answer: “Predicate is commonly used for filtering data, validation, and applying business rules in stream operations.”
10. 🧪 Practical Scenario¶
Problem:¶
Filter users who are:
- Age > 18
- Active status = true
Solution:¶
class User {
int age;
boolean active;
public int getAge() { return age; }
public boolean isActive() { return active; }
}
Predicate<User> isAdult = u -> u.getAge() > 18;
Predicate<User> isActive = u -> u.isActive();
users.stream()
.filter(isAdult.and(isActive))
.forEach(u -> System.out.println(u));
Why this is strong:¶
- Modular conditions
- Easily extendable
How to explain this in interview: “I used multiple Predicate conditions and combined them using and() to filter users based on multiple criteria in a clean way.”
11. 🚀 Interview Tips¶
Common Questions:¶
- What is Predicate?
- Difference between Predicate and Function?
- What is
test()method? - How does chaining work?
Edge Cases:¶
nullinput → can throwNullPointerException- Order of chaining matters in performance
Mistakes:¶
- Writing everything inline instead of reusable predicates
- Overcomplicating conditions
- Not using chaining methods
12. 📝 Summary¶
- Predicate = condition checker
- Takes 1 input → returns boolean
- Method:
test() - Supports chaining:
and,or,negate - Used heavily in Streams API
30-second interview pitch: “Predicate is a functional interface that represents a boolean condition. It takes one input and returns true or false, and is widely used in stream filtering and validation. It also supports chaining using logical operations like and and or.”
🔥 Mock Interview Q&A (with grilling)¶
Q1: What is Predicate?¶
Answer: A functional interface that takes one argument and returns a boolean.
👉 Follow-up: What method does it define? boolean test(T t)
Q2: Predicate vs Function?¶
Answer: Predicate returns boolean, Function returns a value.
👉 Follow-up: Then when would you use Function instead? When transformation is needed instead of condition checking.
Q3: How do you combine predicates?¶
Answer: Using and(), or(), and negate().
👉 Follow-up: Which is evaluated first? Left to right, with short-circuiting behavior.
Q4: What is BiPredicate?¶
Answer: Takes two inputs and returns boolean.
👉 Follow-up: Example? Comparing two values like (a, b) -> a > b.
Q5: Can Predicate throw exceptions?¶
Answer: Not directly—it doesn’t allow checked exceptions.
👉 Follow-up: Then how do you handle it? Wrap in try-catch or use custom functional interface.
Final Pressure Test¶
If I give you:
- A list of objects
- 3 filtering conditions
- Requirement to reuse and combine logic
Can you:
- Write 3 predicates
- Chain them correctly
- Plug into stream
If not, you don’t understand Predicate—you’ve just read about it.