Course Content
Stream API
Stream API
Predicate: Data Filtering
The primary purpose of Predicate
is to facilitate convenient data filtering based on a given condition. This functional interface contains a single abstract method:
Additionally, it provides default methods for combining conditions, such as and()
, or()
, and negate()
, which we will cover later.
Practical Application
The syntax for all functional interfaces follows a similar pattern and is quite simple:
Here, T
represents the type of the input argument, and condition is an expression that returns either true
or false
.
Let's say you need to check whether a given number is even. You can use a Predicate
for this as follows:
Main
package com.example; import java.util.function.Predicate; public class Main { public static void main(String[] args) { // Condition: Check if a number is even Predicate<Integer> isEven = number -> number % 2 == 0; int number = 4; if (isEven.test(number)) { System.out.println(number + " is an even number."); } else { System.out.println(number + " is an odd number."); } } }
This code uses the test()
method of the Predicate
interface, which is designed for evaluating conditions. When test()
is called, it executes the logic defined in the lambda expression number -> number % 2 == 0
, checking if the number is divisible by 2 without a remainder, which determines its evenness.
Here, Predicate
is used to verify whether a given number is even, and the test()
method applies the condition.
Combining Conditions
Predicate
provides the ability to combine conditions using the methods and()
, or()
, and negate()
. This allows for more complex checks. Let's go through some examples:
AND Сondition
Suppose you need to check whether a number meets two criteria: it must be greater than 10 and even.
You can use the and()
method for this, just like &&
operator in Java, to ensure that both conditions must return true
.
Main
package com.example; import java.util.function.Predicate; public class Main { public static void main(String[] args) { // Condition 1: The number is greater than 10 Predicate<Integer> isGreaterThanTen = number -> number > 10; // Condition 2: The number is even Predicate<Integer> isEven = number -> number % 2 == 0; // Combined condition: The number is greater than 10 and even Predicate<Integer> combinedCondition = isGreaterThanTen.and(isEven); int number = 12; if (combinedCondition.test(number)) { System.out.println(number + " meets both conditions."); } else { System.out.println(number + " does not meet the conditions."); } } }
The and()
method combines two conditions. A number must satisfy both criteria for the result to be true
.
OR Сondition
Now, let's say you need to check if a number is either less than 5 or greater than 15.
You can use the or()
method, just like ||
operator in Java, to ensure that at least one of the conditions returns true
.
Main
package com.example; import java.util.function.Predicate; public class Main { public static void main(String[] args) { // Condition 1: The number is less than 5 Predicate<Integer> isLessThanFive = number -> number < 5; // Condition 2: The number is greater than 15 Predicate<Integer> isGreaterThanFifteen = number -> number > 15; // Combined condition: The number is less than 5 or greater than 15 Predicate<Integer> combinedCondition = isLessThanFive.or(isGreaterThanFifteen); int number = 3; if (combinedCondition.test(number)) { System.out.println(number + " meets at least one of the conditions."); } else { System.out.println(number + " does not meet any of the conditions."); } } }
The or()
method checks if a number meets at least one of the given conditions. This is useful when satisfying just one criterion is enough.
NEGATE Сondition
Suppose you need to check whether a string is not empty.
You can use the negate()
method, just like the !
operator in Java, to reverse the result of a condition.
Main
package com.example; import java.util.function.Predicate; public class Main { public static void main(String[] args) { // Condition: The string is empty Predicate<String> isEmpty = str -> str == null || str.isEmpty(); // Condition: The string is not empty Predicate<String> isNotEmpty = isEmpty.negate(); String text = "Hello"; if (isNotEmpty.test(text)) { System.out.println("The string contains text: " + text); } else { System.out.println("The string is empty."); } } }
The negate()
method reverses a condition. If the original condition returns true
, the negated version will return false
, and vice versa.
1. What is the primary purpose of the Predicate
functional interface in Java?
2. What will be the output of the following code?
Thanks for your feedback!