Sitemap
Lavish Jain

Lavish Jain

Software Engineer

Java Built-In Functional Interfaces

2 min readFeb 22, 2022

--

To know about functional interfaces, please first read Java Lambda Expressions.

Java 8 provides a big collection of functional interfaces in the java.util.function package which we can use to define Lambda Expressions without need to define a functional interface explicitly.

Functional Interface Categories

There are 43 functional interfaces provided in java.util.function, and they all fall into one of four broader categories: suppliers, consumers, predicates, or functions.
The basic types of functional interfaces are Supplier, Consumer, Predicate, and Function.

Suppliers

java.util.function.Supplier’s functional method, get(), never takes any argument and always returns something.
Definition of java.util.function.Supplier -

package java.util.function;@FunctionalInterface
public interface Supplier<T> {
T get();
}

Use of Supplier -

Supplier<Integer> supplier = () -> 100;
System.out.println(supplier.get());

There are some variations on Supplier available in java.util.function like IntSupplier, DoubleSupplier, and LongSupplier. These simply supplies an int , a double and a long respectively. These are there primarily used to avoid autoboxing in case you want a primitive, rather than an object, back from the supplier.

Consumers

java.util.function.Consumer’s functional method, accept(), always takes an argument and never returns anything.

Get Lavish Jain’s stories in your inbox

Join Medium for free to get updates from this writer.

Definition of java.util.function.Consumer -

package java.util.function;@FunctionalInterface
public interface Consumer<T> {
void accept(T t);}

Use of Consumer —

Consumer<Integer> consumer = a -> System.out.println(a);
consumer.accept(3);

We also have have java.util.function.BiConsumer that takes two arguments.

package java.util.function;@FunctionalInterface
public interface BiConsumer<T,U> {
void accept(T t, U u);}

ForEach — ForEach is introduced in Java 8. It can be used to iterate through collections. It is a good use case for Consumer as ForEach expects a Consumer.

List<String> list = Arrays.asList("One", "Two", "Three");
Consumer<String> consumer = a -> System.out.println(a);
list.forEach(consumer);

Predicates

java.util.function.Predicate’s functional method, test(), takes a value, does a logical test, and returns true or false.

package java.util.function;@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);}

BiPredicate is another variation of Predicate which takes two arguments instead of one and return boolean.

Functions

java.util.function.Function’s functional method, apply(), takes an argument and returns a value.

package java.util.function;@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}

BiFunction is a variation of Function which takes two arguments

package java.util.function;@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
}

Wrapping Up

These are four broader categories of Functional Interfaces. We have discussed one or two main variations under each category but there are a lot of other variations under each one of four categories discussed.
There are also few functional interfaces already there in JDK, not listed in java.util.function like Comparator with its functional method compare(), Runnable with its functional method run() and so on.

--

--