Predefined Functional Interface in Java 8:-
Java 8 has introduced a set of functional interfaces designed for commonly occurring cases.
For example- testing a condition, processing some object, applying operation on some value and returning a result etc.
All these predefined functional interfaces are present in the package java.util.function.
The package java.util.function contains around 43 predefined functional interface.
However in regular programming the most commonly used functional interfaces are:-
1) Predicate
2) Function
3) Consumer
4) Supplier
Predicate Functional Interface:-
A Predicate is a statement that may be true or false depending on the values of its variables.
In Java 8, Predicate is a functional interface that can be used anywhere we need to evaluate a boolean condition.
It takes an object as argument and returns true or false depending on some condition.
It's common use cases are:-
1) Find all children who were born after a particular date.
2) Pizzas ordered on a specific time.
3) Employees greater than certain age and soon.
Predicate contains 5 methods and they are:-
1) test()
2) and()
3) or()
4) negate()
5) isEqual()
The method test( ) is an abstract method while others methods are default and static.
1. test( ):-
Prototype of test():- Following is the prototype of the method test() as declared in the interface Predicate.
boolean test (T args);
As we can observe the method test() accepts an argument of type T and returns a boolean value.
Here T is a generic type and will be replaced with the class name mentioned while declaring a Predicate reference.
Ex:-
Predicate<String> p1=.......;
In this case the argument type of test () will become String.
Example:-
Suppose we want to check whether a number is Even or Odd.
We can use Predicate for this, by first defining it and then using it for checking the number.
Output:-
8 is Even: true
5 is Even: false
What will happen in the following code?
Output:-
Syntax Error:- incompatible types: String cannot be converted to Integer
Why?
This is because we have used Generics type safety due to which at compile time the compiler is able to detect that a String is being passed where an Integer is expected.
Chaining Predicates:-
Java allows us to join two or more Predicates.
This can be done by calling the following default methods given by Predicate Interface.
These methods are and() and or( ).
2. and( ):-
Prototype of and():- Following is the prototype of the method and() as declared in the interface Predicate.
default Predicate<T> and(Predicate<T> args);
This method and() does logical AND(&&) of the Predicate on which it is called with another Predicate and returns a new Predicate.
Ex:-
p3=p1.and(p2);
The above code will create a new Predicate called p3 which will represent a logical AND of the conditions mentioned in the predicates p1 and p2.
3. or( ):-
Prototype of or():- Following is the prototype of the method or() as declared in the interface Predicate.
default Predicate<T> or(Predicate<T> args);
This method or() does logical Oring (||) of the Predicate on which it is called with another Predicate and returns a new Predicate.
Ex:-
p3=p1.or(p2);
The above code will create a new Predicate called p3 which will represent a logical OR of the conditions mentioned in the predicates p1 and p2.
Example:-
Suppose we have a list of no's as shown below-
Now we want to filter out three types of numbers from the list-
1) No divisible by 2
2) No divisible by 3
3) No divisible by 2 and 3
Solution:-
Output:-
4. negate( ):-
Prototype of negate():- Following is the prototype of the method negate() as declared in the interface Predicate.
default Predicate<T> negate( );
The method negate() does boolean negation of the Predicate on which it is invoked.
Ex:-
p2=p1.negate();
The above code will create a new Predicate called p2 which will represent a boolean negation ( logical reverse) of the conditions mentioned in the Predicate p1.
5. isEqual( ):-
Prototype of isEqual():- Following is the prototype of the method isEqual() as declared in the interface Predicate.
The method isEqual() is a static method which returns a Predicate that tests if two arguments are equal to Objects.equals(Object, Object).
Ex:-
p=Predicate.isEqual("bhopal");
The above code will create a new Predicate called p which will representing the string “bhopal” and whenever we will call test() method on p, it will check whether the argument passed to it is “bhopal” or not.
If it is "bhopal", then true will be returned otherwise false will be returned.
Example:-
Output:-
true
false
0 Comments