Java Function Interface
“ The Function interface generically takes a parameter of type T, executes the given job, and then returns a value of type R. “
- apply() (Abstract Method for Functional Interface) -> Applies this function to the given argument.
- compose() (Default Method)-> Returns a composed function that first applies the before function to its input, and then applies this function to the result.
- andThen() (Default Method) ->Returns a composed function that first applies this function to its input, and then applies the after function to the result.
- identity() (Default Method) -> Returns a function that always returns its input argument.
Simple syntax :
Function<T,R> func = doSomething(return R Type);
func.apply(T type param);
Lets write some examples..
/*
* A Function interface takes the Integer parameter and returns a
* Double type value that returns the Square of the value;
*/
Function<Integer,Double> square = s -> Math.pow(s,2);
System.out.println(square.apply(5));//Output : 25
We can use Unary Operator when the entered parameter and the return type are the same.
Like this ..
UnaryOperator<String> unaryOperator = s -> s.toUpperCase();
System.out.print(unaryOperator.apply("hey"));//Output : HEY
Happy coding ..