If I wrote the Predicate
interface, I'd want to encode in the interface the fact that it's just a function that returns a primitive boolean
, like this:
@FunctionalInterface
public interface Predicate<T> extends Function<T, Boolean> {
boolean test(T t);
@Override
default Boolean apply(T t) {
return Boolean.valueOf(test(t));
}
}
I was wondering, is there a compelling reason Java 8 API designers chose to keep the Predicate
completely separate from Function
? Is there some evidence that they considered doing so and decided against it? I guess similar question goes for all the other 'special' functional interfaces like Consumer
(could be Function<T, Void>
), Supplier
(Function<Void, T>
) and primitive functions like IntFunction
(Function<Integer, T>
).
I haven't thought very deeply and thoroughly about all the ramifications of this, so I'm probably missing something.
EDIT: Some of the answers emphasize the semantic distinction between apply and test. I'm not saying I don't appreciate the distinction, and I agree that it's beneficial to have this distinction. What I don't understand is why a Predicate
is nevertheless not also a Function in
the same way as, e.g., a List
is a Collection
or Double
is a Number
, which is an Object
.
If Predicate
(and all the other special generic functional interfaces, such as Consumer
, Supplier
, IntUnaryOperator
etc.) had this relation with Function
, it would allow one to use it in place where Function
parameter is expected (what comes to mind is composition with other functions, e.g. calling myFunction.compose(myPredicate)
or to avoid writing several specialized functions in an API when such auto(un)boxing implementation as described above would be sufficient)
EDIT 2: Looking at openjdk lambda project I found that primitive functional interfaces used to extend Function
up until this commit from Brian Goetz on 2012-12-19. I couldn't find specific reasons for the change on any of the lambda-dev or JSR experts group mailing lists around that time.
In my opinion
Function<T, R>
is just the definition of a generic function. If allFunctionalInterfaces
would implementFunction
, the only abstract method would have to be namedapply()
. In the context of a concreteFunctionalInterface
likeFilterFile
, the abstract methodboolean accept(File pathname)
is a much better name thenBoolean apply(File)
.The annotation
@FunctionalInterface
already marks an interface as being intended to be usable as aFunctionalInterface
. There is no benefit having them all implement a base interface other then processing them in a generic way. I do not see when you would not care about the semantics of aFunctionalInterface
in advance to make them available to callapply
for them all.The method in
Predicate<T>
returnsboolean
. The method inFunction<T, Boolean>
returnsBoolean
. They are not the same. Although there is autoboxing, Java methods don't use wrapper classes when primitives would do. Also, there are differences likeBoolean
can benull
whileboolean
can't.It's even more different in the case of
Consumer<T>
. The method inConsumer<T>
has return typevoid
, which means it can implicitly return or return usingreturn;
, but the method inFunction<T, Void>
must return usingreturn null;
explicitly.It is not a direct answer to your question, but for what would you be using it for?
Consider the following scenario: You want to map true/false to the list of values for which it is true respectively false.
With your code you could use:
The following however tells me that they have definately thought about something similar, as they offer the partitioning method:
Some reasons I can think about are:
apply()
method available in aPredicate
where you only expect atest()
method.CustomPredicate
contains two types of functionality. It would only add to confusion.There is no need for such a suspicious inheritance hierarchy. These functional interfaces are inter-changable.
This works in both directions. So why should there be an inheritance relationship advertising one specific direction?