functional_interfaces_java
functional_interfaces_java
com/java-8-functional-interfaces
(/)
1 of 12 07/03/25, 4:00 pm
Functional Interfaces in Java | Baeldung https://www.baeldung.com/java-8-functional-interfaces
1. Introduction (/)
Further reading:
Iterable to Stream in Java (/?
post_type=post&p=16559)
The article explains how to convert an Iterable to Stream and why the
Iterable interface doesn't support it directly.
2. Lambdas in Java 8
2 of 12 07/03/25, 4:00 pm
Functional Interfaces in Java | Baeldung https://www.baeldung.com/java-8-functional-interfaces
3. Functional Interfaces
4. Functions
The most simple and general case of a lambda is a functional interface with
a method that receives one value and returns another. This function of a
single argument is represented by the Function interface, which is
parameterized by the types of its argument and a return value:
One of the usages of the Function type in the standard library is the
Map.computeIfAbsent method. This method returns a value from a map by
key, but calculates a value if a key is not already present in a map. To
calculate a value, it uses the passed Function implementation:
3 of 12 07/03/25, 4:00 pm
Functional Interfaces in Java | Baeldung https://www.baeldung.com/java-8-functional-interfaces
The Function interface also has a default compose method that allows us to
combine several functions into one and execute them sequentially:
assertEquals("'5'", quoteIntToString.apply(5));
Since a primitive type can’t be a generic type argument, there are versions
of the Function interface for the most used primitive types double, int, long,
and their combinations in argument and return types:
• IntFunction, LongFunction, DoubleFunction: arguments are of specified
type, return type is parameterized
• ToIntFunction, ToLongFunction, ToDoubleFunction: return type is of
4 of 12 07/03/25, 4:00 pm
Functional Interfaces in Java | Baeldung https://www.baeldung.com/java-8-functional-interfaces
@FunctionalInterface
public interface ShortToByteFunction {
byte applyAsByte(s
short s);
7. Suppliers
This allows us to lazily generate the argument for invocation of this function
using a Supplier implementation. This can be useful if the generation of the
argument takes a considerable amount of time. We’ll simulate that using
Guava’s sleepUninterruptibly method:
6 of 12 07/03/25, 4:00 pm
Functional Interfaces in Java | Baeldung https://www.baeldung.com/java-8-functional-interfaces
(/) -> {
Supplier<Double> lazyValue = ()
Uninterruptibles.sleepUninterruptibly(1000, TimeUnit.MILLISECONDS);
return 9d;
};
Another use case for the Supplier is defining logic for sequence generation.
To demonstrate it, let’s use a static Stream.generate method to create a
Stream of Fibonacci numbers:
8. Consumers
7 of 12 07/03/25, 4:00 pm
Functional Interfaces in Java | Baeldung https://www.baeldung.com/java-8-functional-interfaces
9. Predicates
8 of 12 07/03/25, 4:00 pm
Functional Interfaces in Java | Baeldung https://www.baeldung.com/java-8-functional-interfaces
(/)
List<String> names = Arrays.asList("Angela", "Aaron", "Bob", "Claire",
"David");
In the code above, we filter a list using the Stream API and keep only the
names that start with the letter “A”. The Predicate implementation
encapsulates the filtering logic.
As in all of the previous examples, there are IntPredicate, DoublePredicate
and LongPredicate versions of this function that receive primitive values.
10. Operators
Operator interfaces are special cases of a function that receive and return
the same value type. The UnaryOperator interface receives a single
argument. One of its use cases in the Collections API is to replace all values
in a list with some computed values of the same type:
names.replaceAll(String::toUpperCase);
9 of 12 07/03/25, 4:00 pm
Functional Interfaces in Java | Baeldung https://www.baeldung.com/java-8-functional-interfaces
of all values. With Stream API, we could do this using a collector, but a more
(/)
generic way to do it would be to use the reduce method:
10 of 12 07/03/25, 4:00 pm
Functional Interfaces in Java | Baeldung https://www.baeldung.com/java-8-functional-interfaces
COURSES
SERIES
ABOUT
11 of 12 07/03/25, 4:00 pm
Functional Interfaces in Java | Baeldung https://www.baeldung.com/java-8-functional-interfaces
PRIVACY MANAGER
12 of 12 07/03/25, 4:00 pm