I was just trying to write a functional interface to understand the different use cases.
Looking at the below code which I have written, I understand that I can have different implementations using lambda expressions. Apart from this can anyone show the complex implementations?
Is it possible to use other default method i.e. addLikeOtherWay in lambda expression? if yes how in my example?
Why would I have interface with only one abstract method? What would be the use case of having only single abstract method in my interface?
public class Calculator {
public static void main(String[] args) {
ICalculator i = (int a, int b) -> a + b;
System.out.println(i.add(5, 2));
i = (int a, int b) -> a + b + a;
System.out.println(i.add(5, 2));
}
}
@FunctionalInterface
interface ICalculator {
public int add(int a, int b);
default int addLikeThis(int a, int b) {
return a + b;
}
default int addLikeOtherWay(int a, int b) {
return a + b + a + b;
}
}
"Is it possible to use the default method in a lambda expression?" Yes. In fact, many Functional Interfaces contain default methods. You need one and only one abstract method in an interface for it to be a functional interface, otherwise there would be other interface methods "unimplemented" by the lambda which is not permitted. But here is how the defaults can be applied. The BiFunction interface below was lifted from the API sources, sans JavaDoc.
The following code works because BinaryOperator and UnaryOperator extend BiFunction and Function respectively.
BinaryOperator<Integer> add = (numb1,numb2)->numb1+numb2;
UnaryOperator<Integer> mul = resultFromAdd->resultFromAdd*20;
BinaryOperator<Integer> addThenMul = (numb1,numb2) ->add.andThen(mul).apply(numb1,numb2);
int result = addThenMul.apply(10,20); // produces (10+20)*20 = 600
The following was lifted form the Java API source files.
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
default <V> BiFunction<T, U, V> andThen(
Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t, U u) -> after.apply(apply(t, u));
}
}
In the example code above I could have used BiFunction<Integer,Integer,Integer> and Function<Integer,Integer>. But the *Operator extensions presume the same type for all args so they are easier to use (i.e. less typing).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With