Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Functional Interface in Java 8

This is a follow up question from :: (double colon) operator in Java 8 in which Java allows you to refer to methods using :: operator.

Is it possible to provide some custom Functional Interface that I create and use it with :: operator? And how to do it?

like image 898
Narendra Pathai Avatar asked Nov 18 '13 12:11

Narendra Pathai


People also ask

Why do we need functional interfaces in Java 8?

Objects are the base of java programming language and we can never have a function without an Object, that's why Java language provide support for using lambda expressions only with functional interfaces.

How many functional interfaces does Java 8 have?

A functional interface has only one functionality to exhibit. From Java 8 onwards, we can use lambda expressions to represent the instance of a functional interface. There can be any number of default and static methods in a functional interface that have an implementation.

What is the use of functional interface in real time?

Further, due to the fact that we can use functional interfaces as target types for lambda expressions & methods references, this would be useful when: passing a comparator to a sort method e.g. List. sort , Stream. sorted , Collections.


Video Answer


2 Answers

“Is it possible to provide some custom Functional Interface that I create and use it with :: operator? And how to do it?”

It is possible and it’s as easy as you could imagine: just create an interface with exactly one method. You don’t even need the @FunctionalInterface annotation; this annotation just documents your intention and helps detecting errors at compile time similar to @Override.

So maybe you already have created such interfaces in your pre-Java 8 projects…

class Foo {
    // nothing new:
    public interface FooFactory {
        Foo createFoo();
    }
    // new in Java 8:
    public static final FooFactory DEFAULT_FACTORY = Foo::new;
}
like image 64
Holger Avatar answered Nov 03 '22 01:11

Holger


How to provide custom functional interface implementation to use :: operator

public class TestingLambda {

    public static void main(String[] args) {
        int value1 = method(TestingLambda::customMethod1);
        int value2 = method(TestingLambda::customMethod2);

        System.out.println("Value from customMethod1: " + value1);
        System.out.println("Value from customMethod2: " + value2);
    } 

    public static int customMethod1(int arg){
        return arg + 1;
    }

    public static int customMethod2(int arg){
        return arg + 2;
    }

    public static int method(MyCustomInterface ob){
        return ob.apply(1);
    }

    @FunctionalInterface
    interface MyCustomInterface{
        int apply(int arg);
    }
}

Phase 1: Create a custom functional interface

I have created my own FunctionalInterface named MyCustomInterface and in Java 8 you have to declare the interface to be functional interface using @FunctionalInterface annotation. Now it has a single method taking int as param and returning int.

Phase 2: Create some methods confirming to that signature

Created two methods customMethod1 and customMethod2 which confirm to the signature of that custom interface.

Phase 3: Create a method that takes Functional Interface (MyCustomInterface) as argument

method takes MyCustomInterface in the argument.

And you are ready to go.

Phase 4: Use

In the main I have used the method and passed it the implementation of my custom methods.

method(TestingLambda::customMethod1); 
like image 45
Narendra Pathai Avatar answered Nov 03 '22 02:11

Narendra Pathai