Java Functional Interfaces

In Java a functional interface is one that allows the passing of code as data, and contains (among other things, perhaps, including other methods) a single abstract method. The implementation of such an abstract method is often passed to an algorithm to specify or modify that algorithm's behavior (for example, a sort algorithm that wants to sort in several different ways). Prior to Java 8 this was accomplished by creating a new (possibly anonymous) class that implemented the interface and passing an object of this class to the algorithm in question. With Java 8 this can be achieved more easily by using a lambda function or method reference.

A method reference, in turn, can be any of the following:

In what follows, when we refer to a total ordering on a collection of objects just think of being able to compare any two objects from that collection to see if they are the same, or, if not, which "comes first".

Selected Functional Interfaces and their Abstract Methods (from the Java API)

In the following list of interfaces, Comparable<T> and Supplier<T> contain one method each (the one shown) but each of the other interfaces contains more than one. When implementing any of these interfaces, the only method you absolutely must implement is the first one shown below, since all the other methods are either default or static. One or more of the other methods may of course be useful in your situation, and you may even want or need to override one of those other methods.

java.lang.Comparable<T>
Imposes a total ordering on the objects of each (generic, of type T) class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo() method is referred to as its "natural" comparison method.
- int compareTo(T o) [Compares this object with the specified object for order.]

java.util.Comparator<T>
Provides a comparison function which imposes a total ordering on some collection of objects (of type T).
- int compare(T o1, T o2) [Compares its two arguments for order.]

java.util.function.Predicate<T>
Represents a predicate (boolean-valued function) of one argument.
- boolean test(T t) [Evaluates this predicate on the given argument.]
- default Predicate<T> and() [Returns a predicate that represents this predicate combined with another predicate that is the argument of and().]
- default Predicate<T> or() [Returns a predicate that represents this predicate combined with another predicate that is the argument of or().]
- default Predicate<T> negate() [Returns a predicate that represents the logical negation of this predicate.]
- static <T> Predicate<T> isEqual(Object targetRef) [Returns a predicate that tests if two arguments are equal according to Object.equals(Object, Object).]

java.util.function.Supplier<T>
Represents a supplier of results.
- T get() [Does not have to return a new or distinct result on each call.]

java.util.function.Consumer<T>
Represents an operation that accepts a single input argument (of type T) and returns no result. Unlike most other functional interfaces, it is expected to operate via side-effects.
- void accept(T t) [Performs this operation on the given argument.]
- default Consumer<T> andThen(Consumer<? super T> after) Returns a composed Consumer that performs, in sequence, this operation followed by the after operation.

java.util.function.Function<T, R>
Represents a function that accepts one argument (of type T) and produces a result (of type R).
- R apply(T t) [Applies this function to the given argument.]