Java8 - Functional Interfaces tutorials with examples

What are Java 8 Functional Interfaces?

Functional Interfaces are new concepts introduced in Java 8.

The name itself says it is an interface that contains only abstract methods. Importantly, An instance of this interface is created by lambda expressions, method references and constructor references.

This was introduced to have a concept of functional programming in Java 8.

Java 8, allows creating a custom functional interface as well as using the inbuilt functional interface.

java8 functional interfaces features

  • It contains an only single abstract method, single functionality presented
  • Can be declared many default and static methods
  • Can be declared methods of the object class
  • It can extend the nonfunctional interface
  • It can extend another interface that doesn’t have any abstract methods

java8 @FunctionalInterface annotation example

It is a new annotation introduced for functional interfaces.

It marks the interface for functional programming.

It can be used to check code during the compilation phase. This annotation is not compulsory to use, still, you can use the interfaces in lambda expressions.

java.util.function package contains various inbuilt functional interfaces

Custom Functional Interface Example

This is an example of creating our custom functional interface.

Created MultiplyFunctionalInterface by annotating @FunctionalInterface.

It has a single abstract method, not implementation provided. Written lambda expression to implement the Functional interface

public class FunctionalInterfaceExample {
    public static void main(String[] args) {
        MultiplyFunctionalInterface mfi = (value1, value2) -> value1 * value2;
        System.out.println(mfi.multiply(5, 6));

    }
}

@FunctionalInterface
interface MultiplyFunctionalInterface {
    public Integer multiply(Integer value1, Integer value2);
}

Predefined Functional interfaces example

This is an example of using existing functional interface. LongBinaryOperator is a functional interface in java.util.function package. It takes two values and generates the output as single. applyAsLong method is used in the below example.

import java.util.function.LongBinaryOperator;
public class InbuiltFunctionExamle {
    public static void main(String args[]) {
        LongBinaryOperator add = (value1, value2) -> value1+ value2;
        System.out.println(add.applyAsLong(5, 400));

    }
}

and output:

405

Functional Interface example

We will see valid and invalid functional interface examples

  • Valid Functional interface example

The below example is a valid Functional interface, a method is a single abstract method toString and equals from Object class can be override

@FunctionalInterface
interface MyInterface {
    int method(int a, int b);
    @Override
    public String toString();
    @Override
    public boolean equals(Object obj);
}

  • Invalid Functional interface examples

The below examples are invalid functional interfaces, throws compile-time errors ‘Invalid ‘@FunctionalInterface’ annotation; is not a functional interface’ The below interface two abstract methods, Only a single abstract method allowed.

@FunctionalInterface
interface InvalidFunctionalInterface {
    public Integer method(Integer value1, Integer value2);
    public Integer method1(Integer value1, Integer value2);
}

The functional interface is extending another non-functional interface that has an abstract method. It should be valid if it has does not have an abstract method on it

interface NonFunctionalInterface{
    void method();
}
@FunctionalInterface
interface FunctionalInterfaceHello extends NonFunctionalInterface{
    void method1();
}

java.util.function package interface and implementation classes

java.util.function package in java8 contains various Predefined Functional interfaces.

InterfaceDescription
FunctionThis specifies general function takes one parameter as input and output the result
PredicateOutput of a conditional expression with a single argument
ConsumerIt specifies an operation that takes a single parameter and outputs no result
SupplierIt returns the supplier of result
IntSupplierUsed as a supplier for integer result
DoubleSupplierUsed as a supplier for the Double Result
[LongSupplier](/java8-primitive-supplier-exampleUsed as a supplier for the Long result
BooleanSupplierUsed as a supplier for a Boolean result
IntConsumerIt is a consumer that accepts integer values and returns no result
DoubleConsumerThis interface accepts double values and returns no result
LongConsumerRepresents Consumer that accepts long values and returns no result
UnaryOperatorIt is operand one operand and returns the same result type
IntUnaryOperatorTakes integer argument and returns the integer result
DoubleUnaryOperatorinput is a double argument and returns the double result
LongUnaryOperatorIt takes the long argument as an input and returns the long result
ToIntBiFunctionIt accepts two integer arguments as input and returns the integer result
ToLongBiFunctionIt accepts two long arguments as input and returns the long result
ToDoubleBiFunctionIt accepts two double arguments as input and returns the double result
ToIntFunctionIt accepts any value and returns an only integer value
ToLongFunctionIt accepts any value and returns only long result
IntToLongFunctionIt accepts integer value and returns only long value
ToDoubleFunctionIt accepts any value and returns only double result
LongToIntFunctionIt accepts Long value and returns only integer value
IntToDoubleFunctionIt accepts integer value and returns only double result
DoubleToLongFunctionIt accepts the double value and returns only long value
LongToDoubleFunctionIt accepts the long value and returns only double value
IntFunctionIt accepts integer value and returns data type of given value
LongFunctionIt accepts the long value and returns data type of given value