Functional Interfaces in Java

Functional Interfaces in Java

In Java, being an object oriented programming language, everything revolves around Objects and classes.If we want to write a simple one statement for displaying a message using System.out.print then also we need to write a class to have such method.Means we can’t write any function/method without writing a class in java.

Java hs solved this restriction by introducing Functional interface in java 8.Like functional programming language, java users can use functional interface as an independent entity.

Functional interface contains only one abstract method.It might have default or static methods but it should not contain more than one abstract methods.Abstract method are methods without implementation, we just declare them and leave the implementation for the the sub class to provide.Sub class will provide the implementation for abstract methods by overriding such methods.Functional Interface are also called as Single Abstract Method (SAM) interface.

We mark such interfaces with @FunctionalInterface Annotation.

For example:

Example1:

@FunctionalInterface
interface MyFirstFunctionalInterface {
    void calculate();
}    

Example 2:

@FunctionalInterface
interface MyFirstFunctionalInterface {
    void calculate();
      public default void calculateAndPrint(String input) {
        System.out.println("This is a default method in functional interface");
    }
    public static void print(String input, String input1) throws IOException {
        System.out.println("This is a static method in functional interface");
    }
}

We can call functional interface with 2 ways:

1) By implementing the interface(default way)

2) By lamda expression

1)Using implementation:

class FunctionalInterfaceImpl implements MyFirstFunctionalInterface {
    @Override
    public void calculate() {
        System.out.println("********* Test MyFirstFunctionalInterface *******");
    }
}

Calling the above implementation:

 FunctionalInterfaceImpl functionalInterface = new FunctionalInterfaceImpl();
        functionalInterface.calculate();    

2)Using Lamda expression:

public class FunctionalInterfaceTest {
    public static void main(String a[]) {
        MyFirstFunctionalInterface myFirstFunctionalInterface = () -> {
            System.out.println("********* Test MyFirstFunctionalInterface *******");
        };
        myFirstFunctionalInterface.calculate();
    }
}

Built-in Functional Interfaces in Java

Java has built-in functions written for some common use cases.

Function:
Function interface is similar to a function which take a single parameter as input and return one single value as output.

public interface Function {
    public  apply(T t);
}

Predicate:
Predicate takes one single parameter as input and returns a boolean value(true or false).

public interface Predicate {
    boolean test(T t);
}    

Supplier:
Supplier functional interface taken no input but returns some value as output.

    @FunctionalInterface
public interface Supplier{
    T get();
}

Consumer:
Consumer functional interface takes some input but does not return any return output.

    @FunctionalInterface
public interface Consumer{
    void accept(T t);
}

UnaryOperator:
Input and output types are same in case of UnaryOperator interface.

    T apply(T t)

BinaryOperator:
Binary operator functional interface takes two parameters as input and return a single value as output.

    T apply(T t, T u)

Leave a comment