Functional Interfaces OverviewS2C Home « Functional Interfaces Overview

In this lesson we take an overview of functional interfaces which were introduced in Java8 and are interfaces containing a single abstract method, this does not include declaration of an abstract method overriding one of the public methods of java.lang.Object since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

A functional interface can have any number of default and static methods, as long as there is only a single abstract method it is a functional interface. Functional interfaces can be annotated with the @FunctionalInterface annotation, to ensure that the functional interface can’t have more than one abstract method, although this isn't mandatory. Interfaces annotated with the @FunctionalInterface annotation will be flagged in the compiler if more than one abstract method is present excluding those from java.lang.Object as mentioned above.

The following interfaces that are familiar to Java programmers ActionListener, Callable, Comparable and Runnable are now referred to as functional interfaces from Java8 onwards, as they have only ever had a single abstract method. Before Java 8, we had to create anonymous inner class objects or implement these interfaces to use them but from Java8 they can be used via assignment of a lambda expression or method reference.

The following code example illustrates functional interface usage via assignment, we will go into much greater detail in the Lambda Expressions and Method References lessons.



// Lambda expression
(String s) -> System.out.println(s)

// Method Reference
System.out::println}


Predefined Functional Interfaces Top

Java8 comes with many predefined functional interfaces for us to use 'straight out of the box' and which are listed in the table below.

Functional Interface Description Abstract Method Other Methods
Within java.util.function package
BiConsumer<T,U>Represents an operation that accepts two input arguments and returns no result.acceptandThen (default)
BiFunction<T,U,R>Represents a function that accepts two arguments and produces a result.applyandThen (default)
BinaryOperator<T>Represents an operation upon two operands of the same type, producing a result of the same type as the operands.applyandThen (default)
maxBy (static)
minBy (static)
BiPredicate<T,U>Represents a predicate (boolean-valued function) of two arguments.testand (default)
negate (default)
or (default)
Consumer<T>Represents an operation that accepts a single input argument and returns no result.acceptandThen (default)
Function<T,R>Represents a function that accepts one argument and produces a result.applyandThen (default)
compose (default)
identity (static)
Predicate<T>Represents a predicate (boolean-valued function) of one argument.testand (default)
isEqual (static)
negate (default)
or (default)
Supplier<T>Represents a supplier of results.get
UnaryOperator<T>Represents an operation on a single operand that produces a result of the same type as its operand.applyandThen (default)
compose (default)
identity (static)
Existing interfaces that became functional in Java8
ActionListenerListener for receiving action events.actionPerformed
CallableA task that returns a result and may throw an exception.call
ComparatorComparison function, which imposes a total ordering on some collection of objects.comparereversed (default)
comparing (default)
comparingDouble (default)
comparingInt (default)
comparingLong (default)
thenComparing (static)
thenComparingDouble (static)
thenComparingInt (static)
thenComparingLong (static)
equal
naturalOrder (static)
nullsFirst (static)
nullsLast (static)
reverseOrder (static)
RunnableRuns an action without arguments or return value.run

Primitive Type Specializations Top

When using one of the primitive types double, int or long make use of the available specialization types from the table below to reduce autoboxing.

Functional Interface Description Abstract Method Other Methods
ObjDoubleConsumer<T>
ObjIntConsumer<T>
ObjLongConsumer<T>
Represents an operation that accepts a primitive type and input argument and returns no result.accept
ToDoubleBiFunction<T,U>
ToIntBiFunction<T,U>
ToLongBiFunction<T,U>
Represents a function that accepts a primitive type and input argument and produces a result.applyAsDouble
applyAsint
applyAslong
DoubleBinaryOperator
IntBinaryOperator
LongBinaryOperator
Represents an operation upon a primitive type, producing a result of the same type as the operands.applyAsDouble
applyAsInt
applyAsLong
BooleanSupplier
DoubleSupplier
IntSupplier
LongSupplier
Represents a supplier of primitive valued results.getAsBoolean
getAsDouble
getAsInt
getAsLong
DoubleConsumer
IntConsumer
LongConsumer
Represents an operation that accepts a primitive type and returns no result.acceptandThen (default)
DoubleFunction<R>
DoubleToInt
DoubleToLong
IntFunction<R>
IntToDoubleFunction
IntToLongFunction
LongFunction<R>
LongToDoubleFunction
LongToIntFunction
ToDoubleFunction<T>
ToIntFunction<T>
ToLongFunction<T>
Represents a function that accepts a primitive type and produces a result.apply
applyAsInt
applyAsLong
apply
applyAsDouble
applyAsLong
apply
applyAsDouble
applyAsInt
applyAsDouble
applyAsInt
applyAsLong
DoubleUnaryOperator
IntUnaryOperator
LongUnaryOperator
Represents an operation on a primitive type that produces a result of the same type as its operand.applyAsDouble
applyAsInt
applyAsLong
andThen (default)
compose (default)
identity (static)

Related Quiz

OO Concepts Quiz 13 - Functional Interfaces

Lesson 13 Complete

In this lesson we took an overview of functional interfaces, which were introduced in Java8, and the predefined functional interfaces that are available.

What's Next?

In the next lesson we look at lambda expressions which were also introduced in Java8, their syntax and how to use them.