Java 8 Predicate Interface
7 years ago Lalit Bhagtani 0
In this tutorial, we will learn about Java 8 Predicate interface.
Predicate Interface :-
Predicate ( java.util.function.Predicate ) is a functional interface that has one abstract method and three default method declared in it.
Abstract method :-
boolean test ( T t ) :- The abstract method test ( T t ) is a functional method. It accepts one parameter as an argument and return boolean value. It represents a conditional operation, where a passed argument is tested against a specific value to give a boolean ( True or False ) result.
Default method :-
- default Predicate<T> and ( Predicate <? super T > other ) :- It returns a predicate that represents short circuiting logical AND of this predicate ( predicate on which method is called ) and another ( passed as an argument ).
- default Predicate<T> or ( Predicate <? super T > other ) :- It returns a predicate that represents short circuiting logical OR of this predicate ( predicate on which method is called ) and another ( passed as an argument ).
- default Predicate<T> negate ( ) :- It returns a predicate that represents logical negation of this predicate ( predicate on which method is called ).
Reference :- Predicate Interface JavaDocs
Java 8 Stream API have four methods where Predicate interface are used as an argument, they are as follows :-
- Stream<T> filter ( Predicate<? super T> predicate )
- boolean anyMatch ( Predicate<? super T> predicate )
- boolean allMatch ( Predicate<? super T> predicate )
- boolean noneMatch ( Predicate<? super T> predicate )
There are three specialised ( primitive specific ) form of Java 8 Predicate interface, which can be used in case of int, long and double. They are as follows :-
- IntPredicate interface ( JavaDocs ) :- It represents a predicate that accepts one integer as an argument and returns boolean value.
- DoublePredicate interface ( JavaDocs ) :- It represents a predicate that accepts one double as an argument and returns boolean value.
- LongPredicate interface ( JavaDocs ) :- It represents a predicate that accepts one long as an argument and returns boolean value.
Example – 1 :-
This example will show you, how to create and call different methods of a predicate interface.
public static void main(String[] args){ Predicate<Integer> predicate1 = ( x ) -> { if( x == 5 ){ return true; } else{ return false; }}; // Example of test method boolean result = predicate1.test(5); System.out.println(result); // Example of negate method boolean result3 = (predicate1.negate()).test(5); System.out.println(result3); Predicate<Integer> predicate2 = ( x ) -> { if( x == 7 ){ return true; } else{ return false; }}; // Example of and method boolean result1 = predicate1.and(predicate2).test(5); System.out.println(result1); // Example of or method boolean result2 = predicate1.or(predicate2).test(5); System.out.println(result2); }
Result :-
true false false true
Example – 2 :-
In this example, we will filter the elements of a list by using stream’s filter() method. This method takes one predicate as an argument.
public static void main(String[] args){ List<String> names = new ArrayList<String>(); names.add("Akash"); names.add("Susane"); names.add("Hugh"); names.add("Kim"); names.stream().filter( n -> "Hugh".equals(n)).forEach( n -> System.out.println("Name : " + n)); }
Result :-
Name : Hugh
That’s all for Java 8 Predicate Interface. If you liked it, please share your thoughts in comments section and share it with others too.