In addition to the answer by @assylias which I think solves the problem in most cases, there is one more option, and that is, to make your own @FunctionalInterface
and keep it that way.
It depends on where you use the functions. All of the aforementioned interfaces can be used by JDK utility classes. Predicate
allows filtering, Supplier
allows object creation, Function
allows mapping ... In fact, Predicate
and Supplier
are rather straighforward, but Function
and Consumer
can be often unclear, especially BiFunction
. They can also tie your hands in some use cases.
You can write your own interface that has any amount of inputs, throws checked exceptions, has generics only where you need them and its name says what it should be used for.
//@FunctionalInterface
public interface MyCustomInterface {
<T> MyCustomOutput myCustomAction(MyCustomInput<T> str) throws MyCustomException;
}
So, while the interfaces provided with JDK are useful, sometimes you might prefer to keep your own solution even in Java 8, just with the annotations, and lambdas instead of anonymous classes.