Jigar Naik
Jigar Naik

Reputation: 1994

Functional Interface Implementations and use cases

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;
    }

}

Upvotes: 3

Views: 2436

Answers (2)

Dishonered
Dishonered

Reputation: 8851

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?

To facilitate the use of lambda expressions , which are nameless functions. Lambda expressions make code expressive and reduce clutter. It also makes code more readable. This is based my experience working with lambda expressions.

Upvotes: 0

WJS
WJS

Reputation: 40057

"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).

Upvotes: 1

Related Questions