Predicate is a predefined functional interface in Java used to test a condition and return a boolean result.
In simple words:
Predicate checks whether a condition is true or false.
Main Package
java.util.function
Predicate Introduced In
Predicate functional interface was introduced in:
Java 8
Why Predicate was Introduced?
Before Java 8:
- Condition checking required verbose code
- Filtering collections was complex
- Reusable condition logic was difficult
- Functional-style filtering was not available
Predicate Goal
Input Value
|
v
Condition Applied
|
+-------> TRUE
|
+-------> FALSE
Predicate Declaration
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
Important Point
Predicate contains:
One Abstract Method
called:
test()
Method Signature
boolean test(T t)
How Predicate Works?
- Receives input value
- Applies condition
- Returns true or false
Predicate Working Flow
Input Value
|
v
Predicate Condition
|
+-------> true
|
+-------> false
Basic Predicate Example
Predicate<Integer> even =
n -> n % 2 == 0;
System.out.println(
even.test(10)
);
Output
true
Another Example
Predicate<String> checkLength =
str -> str.length() > 5;
System.out.println(
checkLength.test("Java")
);
Output
false
Predicate with Streams
Predicate is heavily used with Stream API filtering.
Example
List<Integer> even =
numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
Stream Filtering Flow
Stream Elements
|
v
Predicate Applied
|
+-------> TRUE -> Retained
|
+-------> FALSE -> Removed
Common Predicate Methods
| Method | Purpose |
|---|---|
| test() | Checks condition |
| and() | Logical AND |
| or() | Logical OR |
| negate() | Logical NOT |
| isEqual() | Equality check |
1. test()
Evaluates condition.
Example
Predicate<Integer> positive =
n -> n > 0;
System.out.println(
positive.test(5)
);
Output
true
2. and()
Combines two predicates using logical AND.
Example
Predicate<Integer> positive =
n -> n > 0;
Predicate<Integer> even =
n -> n % 2 == 0;
Predicate<Integer> result =
positive.and(even);
System.out.println(
result.test(10)
);
AND Flow
Condition 1 = TRUE
|
v
Condition 2 = TRUE
|
v
Final Result = TRUE
3. or()
Combines predicates using logical OR.
Example
Predicate<Integer> even =
n -> n % 2 == 0;
Predicate<Integer> greater =
n -> n > 100;
Predicate<Integer> result =
even.or(greater);
4. negate()
Reverses predicate result.
Example
Predicate<Integer> even =
n -> n % 2 == 0;
Predicate<Integer> odd =
even.negate();
Negate Flow
TRUE
|
v
negate()
|
v
FALSE
5. isEqual()
Checks object equality.
Example
Predicate<String> equal =
Predicate.isEqual("Java");
System.out.println(
equal.test("Java")
);
Output
true
Predicate Chaining
Predicates can be combined together.
Example
Predicate<Integer> result =
positive
.and(even)
.negate();
Predicate Chaining Flow
Input
|
v
Predicate 1
|
v
Predicate 2
|
v
negate()
|
v
Final Boolean Result
Predicate in Banking Systems
Banking applications use Predicate for:
- Fraud detection rules
- Transaction validation
- Risk analysis
- Loan eligibility checks
- Compliance filtering
Banking Flow
Transaction Data
|
v
Predicate Rules Applied
|
+-------> Suspicious
|
+-------> Valid
Predicate in E-Commerce Systems
E-commerce platforms use Predicate for:
- Product filtering
- Recommendation systems
- Customer segmentation
- Discount eligibility
- Inventory filtering
E-Commerce Flow
Products Stream
|
v
Predicate Filters Applied
|
v
Matching Products Displayed
Predicate in Spring Boot
Spring Boot applications heavily use Predicate for:
- REST filtering
- Business validations
- DTO filtering
- Security rules
- Search APIs
Spring Boot Example
List<User> activeUsers =
users.stream()
.filter(User::isActive)
.collect(Collectors.toList());
Predicate in Microservices
Microservices architectures use Predicate for:
- Distributed filtering
- Reactive event processing
- Cloud-native validations
- Service rule engines
- API gateway filtering
Microservice Flow
Distributed Events
|
v
Predicate-Based Filtering
|
v
Valid Events Processed
Advantages of Predicate
- Cleaner condition logic
- Reusable filtering rules
- Functional programming support
- Easy stream integration
- Improved readability
Disadvantages
- Complex predicate chaining reduces readability
- Debugging nested predicates is difficult
- Overuse may confuse beginners
Common Interview Mistake
Many developers think Predicate returns any datatype.
Actually:
- Predicate always returns boolean.
Another Common Mistake
Many developers confuse Predicate and Function.
Actually:
- Predicate returns boolean.
- Function returns transformed value.
Predicate vs Function
| Feature | Predicate | Function |
|---|---|---|
| Return Type | boolean | Any Type |
| Main Purpose | Condition Checking | Transformation |
| Main Method | test() | apply() |
Best Practices
- Keep predicates simple
- Reuse predicates when possible
- Use method references for readability
- Avoid deeply nested predicate chains
- Use meaningful predicate names
- Prefer immutable filtering logic
Realtime Enterprise Example
Global Fraud Detection Platform
Millions of Transactions
|
v
Predicate Fraud Rules
|
+-------> Suspicious Queue
|
+-------> Normal Processing
Related Learning Topics
- What is Functional Programming in Java
- What is Functional Interface in Java
- What is Lambda Expression in Java
- What is Stream API in Java
- What is map() and flatMap() in Streams
- What is reduce() in Java Streams
- What is Consumer Functional Interface
- What is Spring Boot
Professional Interview Answer
Predicate is a predefined functional interface available in the java.util.function package introduced in Java 8. It represents a boolean-valued function that accepts one input parameter and returns either true or false. Predicate is primarily used for condition checking, filtering, validation, and rule evaluation in functional programming and Stream API operations. The main abstract method of Predicate is test(), and it also provides utility methods such as and(), or(), negate(), and isEqual() for composing complex conditions. Enterprise applications, Spring Boot systems, banking platforms, distributed microservices, analytics engines, cloud-native architectures, and e-commerce systems heavily use Predicate for filtering data, fraud detection, validation pipelines, event processing, business rules, and scalable stream processing. Modern Java development combines Predicate with Stream API, lambda expressions, functional interfaces, reactive programming, and microservices architectures to build clean, maintainable, and scalable enterprise applications.
Frequently Asked Questions
What is Predicate functional interface in Java?
Predicate is a functional interface used for condition checking and returns boolean result.
Which package contains Predicate?
java.util.function
What is the main method in Predicate?
test()
What does Predicate return?
Predicate always returns true or false.
Where is Predicate used?
Stream filtering, validations, fraud detection, business rules, Spring Boot applications, and enterprise Java systems.