{

Java8 - Functional Interfaces tutorials with examples


Java8 functional interface example

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.

Interface Description
Function This specifies general function takes one parameter as input and output the result
Predicate Output of a conditional expression with a single argument
Consumer It specifies an operation that takes a single parameter and outputs no result
Supplier It returns the supplier of result
IntSupplier Used as a supplier for integer result
DoubleSupplier Used as a supplier for the Double Result
[LongSupplier](/java8-primitive-supplier-example Used as a supplier for the Long result
BooleanSupplier Used as a supplier for a Boolean result
IntConsumer It is a consumer that accepts integer values and returns no result
DoubleConsumer This interface accepts double values and returns no result
LongConsumer Represents Consumer that accepts long values and returns no result
UnaryOperator It is operand one operand and returns the same result type
IntUnaryOperator Takes integer argument and returns the integer result
DoubleUnaryOperator input is a double argument and returns the double result
LongUnaryOperator It takes the long argument as an input and returns the long result
ToIntBiFunction It accepts two integer arguments as input and returns the integer result
ToLongBiFunction It accepts two long arguments as input and returns the long result
ToDoubleBiFunction It accepts two double arguments as input and returns the double result
ToIntFunction It accepts any value and returns an only integer value
ToLongFunction It accepts any value and returns only long result
IntToLongFunction It accepts integer value and returns only long value
ToDoubleFunction It accepts any value and returns only double result
LongToIntFunction It accepts Long value and returns only integer value
IntToDoubleFunction It accepts integer value and returns only double result
DoubleToLongFunction It accepts the double value and returns only long value
LongToDoubleFunction It accepts the long value and returns only double value
IntFunction It accepts integer value and returns data type of given value
LongFunction It accepts the long value and returns data type of given value
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.