PC: Scott Webb

Understanding Functional Interfaces in JAVA

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; };

--

--

--

Backend Software Engineer, Technology Enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to use Matplotlib as the Plotting Library

png

Solution : “npm command not found” even after installation (MacBook)

Football Transfers Graph — Linked Data Edition

Using Light Probes in Unity’s HDRP

Blender 2.8 : Auto Merging and Snapping Tools

What software developers can learn from the wizard the OZ

Technical Development Progress Update #7 29 Dec

3 Ways to Center a Div with CSS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rony Emrul

Rony Emrul

Backend Software Engineer, Technology Enthusiast

More from Medium

The basics of immutable classes in Java

Introduction to Project Reactor in Java

Using the Java Library

Functional Java CodeStyle [Part: 2]