Java 8 provides several functional interfaces in package java.util.function
.
For each basic function (Function, Consumer, Predicate, Supplier...) there are other defined where the type parameter is specialized for the following primitive type: double, int, long.
This questions is about the motivation of such interfaces: Why are there primitive functions like DoubleFunction in Java 8
But why not all the primitive types are covered (e.g. float is missing)?
Package java. util. function. Functional interfaces provide target types for lambda expressions and method references. Each functional interface has a single abstract method, called the functional method for that functional interface, to which the lambda expression's parameter and return types are matched or adapted.
The Function Interface is a part of the java. util. function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one argument and produces a result.
A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods.
In Java 8, there are 4 main functional interfaces are introduced which could be used in different scenarios.
This was decided in order to prevent API size explosion. Being forced to introduce primitive specializations at all is already a pain point, so the compromise was to specialize only for the essential types, which are long
and double
, and additionally for int
as the most prominent primitive type: the type of array indices and integer literals. All other types can be promoted to these.
In this post on the lambda-dev mailing list you can read the official statement from Brian Goetz.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With