Java 8 – Functional Interfaces

Functional Interfaces is one of the new important things of Java 8. In previous article, we had a little view about common use cases of Lambda Expression including Runnable and ActionListener. They are example of Functional Interfaces.

This tutorial helps you have deeper concept of Functional Interface, then some interfaces which are provided in java.util.function package such as Predicate and Function.

I. Functional Interfaces

An interface with only one abstract method inside is Functional Interface.

We add @FunctionalInterface annotation to annotate that an interface is Functional Interface. It can be used for making compiler errors when the interface is not a valid Functional Interface.
For example, when we have more than one abstract method inside, it throws a compiler error:

Invalid '@FunctionalInterface' annotation; MyFuncInterface is not a functional interface
So, we can apply them simply:

For Functional Interface with only one method, we can make code lean and beautiful with Lambda Expression, it seem like that we pass a method as argument to a function. It is the convenience when using that kind of interface – take advantage of Java 8 Lambda Expression.

In previous article: Java 8 – Lambda Expressions, we have apply Lambda Expression for two Java Functional Interfaces:
Runnable with single abstract method run()
ActionListener with single abstract method actionPerformed(…)

II. java.util.function Package

Java 8 provides the java.util.function package with a number of standard functional interfaces which are designed as a starter set for developers, these are some of them:
Predicate<T>: Represents a predicate (boolean-valued function) of one argument, functional method: test(Object).
Consumer<T>: Represents an operation that accepts a single input argument and returns no result, functional method: accept(Object).
Function<T,R>: Represents a function that accepts one argument and produces a result, functional method: apply(Object).
Supplier<T>: Provides an instance of a T (such as a factory), functional method: get().

This tutorial introduces 2 functional interfaces: Predicate and Function.

1. Predicate

Predicate interface has method test(Object) to return a boolean value. The idea for this functional interface is that an object is tested to be true or false.

We use Predicate<T> interface to check if a number in list is suitable for specific condition by method test(T t), and the function checkList receive Predicate<Integer> object as an input parameter:

Lambda expression is used to assign method operation to Predicate object or just as an input parameter:

Run these statement, we will have the result:

2. Function

Function<T,R> interface takes a generic class T and returns a generic class R by the method apply(T t);

To be clear, look at the example below.
This is Customer class:

To make the print method more flexible than toString() with the modified printing function input, we have this method:

A Function f will be passed to the method and a String will be returned corresponding to Customer. The method apply processes a lambda expression which determines what kind of Customer information is returned.

This is the testing code:

And the result in Console Window:

III. Source code

Technology:
– Java 8
– Eclipse Mars.1 Release (4.5.1)
funtionalinterface


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*