Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why java.util.function doesn't define specialized functional interfaces for all primitive types?

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)?

like image 545
Paolo Fulgoni Avatar asked Jul 11 '14 07:07

Paolo Fulgoni


People also ask

What functional interface is defined in the Java Util function package?

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.

Is function a functional interface in Java?

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.

How do you define a functional interface in Java?

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.

How many types of functional interfaces are there in Java 8?

In Java 8, there are 4 main functional interfaces are introduced which could be used in different scenarios.


1 Answers

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.

like image 106
Marko Topolnik Avatar answered Oct 07 '22 07:10

Marko Topolnik