AJinkya Ranade (Aj)
4 min readJul 31, 2018

Functional Interface with Lambda Expression.

Hello JAVA 8 Developers !!!

Today I am going to talk about Functional Interface with lambdas Expressions. I have gone through many articles about functional Interface and lambdas expression. Its just talks about function Interface Is a Interface having SAM , default and some static methods. So that’s It ? Does Functional Interface only limited for this? No! It’s more than what we are thinking. Lets explore and some interesting things which are associated with functional Interface with real time example in more casual way.

First try to understand how the Functional Interface is associated with the lambda expression, with help of BiFunction Interfaces.

Declaration : BiFunctional <T , T , R> biFunction ;

By using definitions of BiFunction interface, we can easily create reference variable of It but main questions is who implement this Interfaces and where and how to use It? Right. Answer of this questions is lambda expression. Bit Confusing??? let’s simplify this scenario with simple example.

In above example, you can see the lambda expression,but here It Is not adding two numbers. javac used a definition of SAM for create method internally to add two numbers & code you have written after lambda sign (->) It as a body of this method.
biFunction Is just a reference variable It never holds result of lambda expression . It’s a block or code to call a method created by javac using apply method of BiFunction Interface for getting a result..

Functional interface is a interface having SAM that is used as a type of a lambda expression. 👍

Now we have got familiar with the functional Interface. lets take a step forward & try to write the code in functional programming style with
help of Functional Interface.
once, I had wrote an Interesting code , when my Manager Mr. John called me & asked could you write a code to merge two Map? 😞 using Functional Interface it sounded Interesting!!! Said yes, within a second.
Lets knuckle down in our task , we have to merge two maps, If key are not associated with value then create new list and added element in newly created list otherwise added into existing list against the same key. So guys, any Idea? How we can achieve this? Any click !! Yes , you are right. we can use BiFunction Interface here.

Lets declare variable of BiFunction Interface first

Here I am going to use forEach , computeIfAbsent consent to merge two map.
I have skipped forEach method for now lets concentrate on computeIfAbsent(String ,Function<T,R>) method . computeIfAbsent required two arguments. The second argument is type of Function Interface, so we have two choices here either you can create reference variable or used lambda expression.

Function<String, LinkedList<Long>> function = (k) -> {
return new LinkedList<Long>();
};
or
(k) -> { return new LinkedList<Long>();};

I am choosing 1st way to write a code. Guys any guesses why I have chosen first one ?and not second one? Think on It & let me complete our code also.

Code :

Guys, are you ready with your answer, I have asked ? 😃 nope. Answer of this question Is Function Composition using default methods provided by function Interface.

What is the Function Composition ?
we can create reusable functions and put them together to create a new function is called as function composition. using andThen,compose method of Function interface we can compose the multiple function & decided execution order as well. 👍
guys got back to our task again . I had received a call from Mr. John , he said our requirement get changed we show only the product which start with “A” letter and apply 10% discount on each product. what we can do here, create two different reusable function and put them together using andThen and compose method.

//composed function
result = mergeMethod.andThen(filterMap.compose(applyDiscount))
.apply(map1, map2);

Let’s try to draw flow diagram to simplify above code for more clarity.

Compose & AndThen Execution Order

The difference between andThen and compose method is order of execution.
AndThen : Execute caller first then parameter
compose : Execute parameter first then caller.

Point To Remember
1
Making function nicer, you just move this function inside the class and mark as static so we can used throughout our application.

2 please don’t create function/variable if you have two or three lines of lambda expression.

3 Default method not only used for backward compatibility but also use for Functional composition. Avoid to write to many default method inside the interface It’s not a good architectural design It show compromise viewed.

That’s It for Today but we aren’t done yet !! next we’ll be explore spring boot features with Micro-Service Architecture. Thank you for Reading 😅 😃
please 👏 if you found it helpful.

AJinkya Ranade (Aj)
AJinkya Ranade (Aj)

No responses yet