Java Function Interface

Yigit Kader
1 min readMar 7, 2022

--

“ 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 ..

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Yigit Kader
Yigit Kader

Written by Yigit Kader

Software Engineer , Berlin , @devyigitk twitter

No responses yet

Write a response