PC: Scott Webb

Understanding Functional Interfaces in JAVA

Rony Emrul

--

With the release of JAVA 1.8, it introduces functional programming through lambda expression. Functional Interface is a big part of lambda expression in JAVA because we cannot achieve functional programming without implementing a functional interface.

What is a Functional Interface?

Functional interfaces are those interfaces that have only one abstract method in it. It may contain default methods and static methods, but without only one abstract method we can’t call an interface a functional interface.

In the below example, we have created a functional interface MyFunctionalInterface which contains a single abstract method and we implemented it by a lambda expression.

Output: Result → 30

It is not wise to create a functional interface every time we do something with lambda expressions as Java 8 by default comes with a set of built-in functional interfaces for most of the common cases.

Built-in Functional Interfaces

We can find all the built-in functional interfaces under java.util.function.Function package.

Function

The Function<T,R> Interface represents a function that accepts one argument and produces a result. It has got an abstract method R apply(T t) that takes a specific value as a parameter and returns a specific type of value. Here is an example:

Output: Result — -> 400

Consumer

As the name suggests Consumer<T> interface represents a function that consumed the input passed to it. It has got the abstract method void accept(T t) takes a single input and returns nothing. Here is an example:

Output →
Dhaka
London
Manchester

Supplier

The Java Supplier<T> interface is a functional interface that represents a function that supplies a value of some sorts. The Supplier<T> interface can also be thought of as a factory interface. The T get() method of Supplier interface has no parameter and it returns a result. Here is an example implementation of the Java Supplier interface. For example,

Output: 1.5787219372033863

Predicate

Predicate<T> Represents a predicate (boolean-valued function) of one argument. The boolean test(T t) method of Predicate<T> interface takes a single value as a parameter and returns true or false. It is widely used to This is mainly used to filter data from a Java Stream. Let’s see it with an example,

Output → Barisal
Baridhara
Banasri

UnaryOperator

The Java UnaryOperator<T> interface is a functional interface that represents an operation which takes a single parameter and returns a parameter of the same type. Here is an example of a Java UnaryOperator implementation:

UnaryOperator<Person> unaryOperator = 
(person) -> { person.name = "New Name"; return person; };

The UnaryOperator interface can be used to represent an operation that takes a specific object as a parameter, modifies that object, and returns it again - possibly as part of a functional stream processing chain.

BinaryOperator

The Java BinaryOperator<T> interface is a functional interface that represents an operation which takes two parameters and returns a single value. Both parameters and the return type must be of the same type.

The Java BinaryOperator<T> interface is useful when implementing functions that sum, subtract, divide, multiply etc. two elements of the same type, and returns a third element of the same type.

Here is an example implementation of the BinaryOperator interface:

BinaryOperator<MyValue> binaryOperator =
(value1, value2) -> { value1.add(value2); return value1; };

--

--

Rony Emrul
Rony Emrul

Written by Rony Emrul

Backend Software Engineer, Technology Enthusiast

No responses yet