Predicate is a functional interface that takes a Type T argument and returns a boolean.
public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
....
}
Example 1 : test()
Passing an argument to test()
returns a boolean.
import java.util.function.Predicate;
public class PredicateExample1 {
public static void main(String[] args) {
Predicate<Integer> predicate = (num) -> num > 10;
boolean result = predicate.test(100);
System.out.println(result);
}
}
Output:
true
Example 2 : and()
and()
is a method that chains Predicates. Performs AND operation on the results of Predicates and returns the result.
In the example below, predicate1.and(predicate2).test(25)
returns true when both predicate1 and predicate2 are true. If either of them is false, false is returned.
import java.util.function.Predicate;
public class PredicateExample2 {
public static void main(String[] args) {
Predicate<Integer> predicate1 = (num) -> num > 10;
Predicate<Integer> predicate2 = (num) -> num < 20;
boolean result = predicate1.and(predicate2).test(25);
System.out.println("25 : 10 < num < 20 ? " + result);
result = predicate1.and(predicate2).test(15);
System.out.println("15: 10 < num < 20 ? " + result);
}
}
Output:
25 : 10 < num < 20 ? false
15: 10 < num < 20 ? true
Example 3 : or()
or()
is a method that connects Predicates like and()
. However, or()
returns the result of the OR operation of Predicates.
In the example below, predicate1.or(predicate2).test(5)
returns True if any one of predicate1 and predicate2 becomes True.
import java.util.function.Predicate;
public class PredicateExample5 {
public static void main(String[] args) {
Predicate<Integer> predicate1 = (num) -> num > 10;
Predicate<Integer> predicate2 = (num) -> num < 0;
boolean result = predicate1.or(predicate2).test(5);
System.out.println("5 : num < 0 or num > 10 ? " + result);
result = predicate1.or(predicate2).test(15);
System.out.println("15 : num < 0 or num > 10 ? " + result);
result = predicate1.or(predicate2).test(-5);
System.out.println("-5 : num < 0 or num > 10 ? " + result);
}
}
Output:
5 : num < 0 or num > 10 ? false
15 : num < 0 or num > 10 ? true
-5 : num < 0 or num > 10 ? true
Example 4 : isEqual()
isEqual()
can also be used on Streams, it checks if it is equal to the object passed as an argument.
import java.util.function.Predicate;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class PredicateExample4 {
public static void main(String[] args) {
Stream<Integer> stream = IntStream.range(1, 10).boxed();
stream.filter(Predicate.isEqual(5))
.forEach(System.out::println);
}
}
Output:
5
Example 5 : negate()
negate()
returns a Predicate that returns the opposite of the value returned by the Predicate.
You can think of the logical operator NOT as predicated before the predicate. It can be used to create logically opposing Predicates.
import java.util.function.Predicate;
public class PredicateExample6 {
public static void main(String[] args) {
Predicate<Integer> predicate = (num) -> num > 10;
Predicate<Integer> negatePredicate = predicate.negate();
boolean result = predicate.test(100);
System.out.println("100 is greater than 10 ? " + result);
result = negatePredicate.test(100);
System.out.println("100 is less than 10 ? " + result);
}
}
Output:
100 is greater than 10 ? true
100 is less than 10 ? false
Example 6 : Stream
Predicate can be passed as an argument to filter()
of Stream.
import java.util.function.Predicate;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class PredicateExample3 {
public static void main(String[] args) {
Stream<Integer> stream = IntStream.range(1, 10).boxed();
Predicate<Integer> predicate = num -> num > 5;
stream.filter(predicate)
.forEach(System.out::println);
}
}
Output:
6
7
8
9
References
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)