Java 8 Functional Interfaces
A functional interface is an interface that has exactly one abstract method but can have multiple non-abstract methods.
A functional interface can be implicitly converted to a lambda expression.
Lambda expressions and method references (which can actually also be regarded as lambda expressions).
For example, a functional interface is defined as follows:
@FunctionalInterface
interface GreetingService
{
void sayMessage(String message);
}
Then a lambda expression can be used to represent an implementation of this interface (note: before JAVA 8, this was generally implemented with an anonymous class):
GreetingService greetService1 = message -> System.out.println("Hello " + message);
Functional interfaces provide friendly lambda support for existing functions.
Functional interfaces that existed before JDK 1.8:
- java.lang.Runnable
- java.util.concurrent.Callable
- java.security.PrivilegedAction
- java.util.Comparator
- java.io.FileFilter
- java.nio.file.PathMatcher
- java.lang.reflect.InvocationHandler
- java.beans.PropertyChangeListener
- java.awt.event.ActionListener
- javax.swing.event.ChangeListener
Function interfaces newly added in JDK 1.8:
- java.util.function
java.util.function contains many classes that support Java's functional programming; the functional interfaces in this package are:
| No. | Interface & Description |
|---|---|
| 1 | BiConsumer Represents an operation that accepts two input arguments and returns no result |
| 2 | BiFunction Represents a method that accepts two input arguments and returns a result |
| 3 | BinaryOperator Represents an operation on two operands of the same type that returns a result of the same type as the operands |
| 4 | BiPredicate Represents a two-argument boolean-valued method |
| 5 | BooleanSupplier
Represents a supplier of boolean-valued results |
| 6 | Consumer Represents an operation that accepts one input argument and returns no result |
| 7 | DoubleBinaryOperator
Represents an operation on two double-valued operands that returns a double-valued result. |
| 8 | DoubleConsumer
Represents an operation that accepts a double-valued argument and returns no result. |
| 9 | DoubleFunction Represents a method that accepts a double-valued argument and returns a result |
| 10 | DoublePredicate
Represents a boolean-valued method with a double-valued argument |
| 11 | DoubleSupplier
Represents a supplier of double-valued results |
| 12 | DoubleToIntFunction
Accepts a double input and returns an int result. |
| 13 | DoubleToLongFunction
Accepts a double input and returns a long result |
| 14 | DoubleUnaryOperator
Accepts an argument of type double and returns a value of type double. |
| 15 | Function Accepts one input argument and returns a result. |
| 16 | IntBinaryOperator
Accepts two arguments of type int and returns a value of type int. |
| 17 | IntConsumer
Accepts an int input argument and returns no value. |
| 18 | IntFunction Accepts an int input argument and returns a result. |
| 19 | IntPredicate
: Accepts an int input argument and returns a boolean result. |
| 20 | IntSupplier
Takes no arguments and returns an int result. |
| 21 | IntToDoubleFunction
Accepts an int input and returns a double result. |
| 22 | IntToLongFunction
Accepts an int input and returns a long result. |
| 23 | IntUnaryOperator
Accepts an argument of type int and returns a value of type int. |
| 24 | LongBinaryOperator
Accepts two arguments of type long and returns a value of type long. |
| 25 | LongConsumer
Accepts a long input argument and returns no value. |
| 26 | LongFunction Accepts a long input argument and returns a result. |
| 27 | LongPredicate
Accepts a long input argument and returns a boolean-typed result. |
| 28 | LongSupplier
Takes no arguments and returns a long-typed result. |
| 29 | LongToDoubleFunction
Accepts a long input and returns a double result. |
| 30 | LongToIntFunction
Accepts a long input and returns an int result. |
| 31 | LongUnaryOperator
Accepts an argument of type long and returns a value of type long. |
| 32 | ObjDoubleConsumer Accepts an object-typed and a double-typed input argument and returns no value. |
| 33 | ObjIntConsumer Accepts an object-typed and an int-typed input argument and returns no value. |
| 34 | ObjLongConsumer Accepts an object-typed and a long-typed input argument and returns no value. |
| 35 | Predicate Accepts one input argument and returns a boolean result. |
| 36 | Supplier Takes no arguments and returns a result. |
| 37 | ToDoubleBiFunction Accepts two input arguments and returns a double result |
| 38 | ToDoubleFunction Accepts one input argument and returns a double result |
| 39 | ToIntBiFunction Accepts two input arguments and returns an int result. |
| 40 | ToIntFunction Accepts one input argument and returns an int result. |
| 41 | ToLongBiFunction Accepts two input arguments and returns a long result. |
| 42 | ToLongFunction Accepts one input argument and returns a long result. |
| 43 | UnaryOperator Accepts an argument of type T and returns a value of type T. |
Functional Interface Example
Predicate
This interface contains various default methods to combine Predicates into other complex logic (such as and, or, not).
This interface is used to test whether an object is true or false.
We can learn about the functional interface Predicate through the following example (Java8Tester.java)
Java8Tester.java file
import
java
.
util
.
Arrays
;
import
java
.
util
.
List
;
import
java
.
util
.
function
.
Predicate
;
public
class
Java8Tester
{
public
static
void
main
(
String
args
[
]
)
{
List
<
Integer
list
=
Arrays.
asList
(
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
)
;
//
Predicate
//
n is a parameter passed to the Predicate interface's test method
//
n If it exists, the test method returns true
System
.
out
.
println
(
"
Output all data:
"
)
;
//
Pass parameter n
eval
(
list
,
n
->
true
)
;
//
Predicate
//
n is a parameter passed to the Predicate interface's test method
//
If n%2 is 0, the test method returns true
System
.
out
.
println
(
"
Output all even numbers:
"
)
;
eval
(
list
,
n
->
n
%
2
==
0
)
;
//
Predicate
//
n is a parameter passed to the Predicate interface's test method
//
If n is greater than 3, the test method returns true
System
.
out
.
println
(
"
Output all numbers greater than 3:
"
)
;
eval
(
list
,
n
->
n
3
)
;
}
public
static
void
eval
(
List
<
Integer
list
,
Predicate
<
Integer
predicate
)
{
for
(
Integer
n:
list
)
{
if
(
predicate
.
test
(
n
)
)
{
System
.
out
.
println
(
n
"
"
)
;
}
}
}
}
Executing the above script produces the following output:
$ javac Java8Tester.java
$ java Java8Tester
Output all data:
1
2
3
4
5
6
7
8
9
# Print all even numbers:
2
4
6
8
# Output all numbers greater than 3:
4
5
6
7
8
9
Java 8 New Features