Java 1.8 onwards defines few Pre-defined Functional interfaces to deal with functional programming using lambda expressions and method references.
These Functional Interfaces are:
- Predicate
- Function
- Consumer
- Supplier
- Bi-Functional Interfaces
- Primitive Type Functional Interfaces (to work with primitive types.)
Property | Predicate | Function | Consumer | Supplier |
---|---|---|---|---|
Purpose | To take some input and perform some conditional checks | To take some input and perform required operation and return the result | To consume some input and perform required operation. It doesn't returns anything | To supply some value based on requirements. |
Interface declaration | interface Predicate<T>{ |
interface Function<T,R>{ |
interface Consumer<T>{ |
interface Supplier<R>{ |
SAM (Single Abstract Method) | public boolean test(T t); |
public R apply(T t); |
public void accept(T t); |
public R get(); |
Default Method(s) | and() , or() , negate() |
andThen() , compose() |
andThen() |
- |
Static Method(s) | isEqual() |
identity() |
- |
Normal Functional Interfaces (Predicate, Function, Consumer) can accept only one input argument. But, sometimes Programming requirement is to accept two input arguments, for this Bi Functional Interfaces are present.
These are same as One Argument Functional Interfaces in functionality; these just accept two input parameters instead of one.
Following are the Bi Functional Interfaces present in java.util.function
package.
- BiPredicate
- BiFunction
- BiConsumer
One Argument Functional Interface | Two Argumet Functional Interface |
---|---|
@FunctionalInterface |
@FunctionalInterface |
@FunctionalInterface |
@FunctionalInterface |
@FunctionalInterface |
@FunctionalInterface |
In case of normal Predefined Functional Interfaces the input and return types are always Object types.
If Primitive types are passed to them, the Primitives will be converted to Object types (Autoboxing) and that Object types will further be converted to Primitive types (Autounboxing) for processing,
and finally the result (Primitive type) may finally be converted to Object type (Autoboxing).
This to and fro conversion causes performance issues.
To overcome this issue Primitive functional interfaces are present to work on Primitive types.
- IntPredicate
- LongPredicate
- DoublePredicate
Interface | Purpose | SAM |
---|---|---|
IntPredicate | Always accepts input value of int type | public boolean test(int i); |
LongPredicate | Always accepts input value of long type | public boolean test(long l); |
DoublePredicate | Always accepts input value of double type | public boolean test(double d); |
Takes primitive input and returns any type:
- IntFunction
- LongFunction
- DoubleFunction
Takes any type as input but returns primitive type: 4. ToIntFunction 5. ToLongFunction 6. ToDoubleFunction
Take and returns primitive type: 7. IntToLongFunction 8. IntToDoubleFunction 9. LongToIntFunction 10. LongToDoubleFunction 11. DoubleToIntFunction 12. DoubleToLongFunction
BiFunction, Takes any types as two inputs but always return primitive type: 13. ToIntBiFunction 14. ToLongBiFunction 15. ToDoubleBiFunction
UnaryOperator (Function that takes and return same type), Takes and returns primitive type: 16. IntUnaryOperator 17. LongUnaryOperator 18. DoubleUnaryOperator
BinaryOperator (BiFunction that takes and return same type), Takes and returns primitive type: 19. IntBinaryOperator 20. LongBinaryOperator 21. DoubleBinaryOperator
Interface | Purpose | SAM |
---|---|---|
IntFunction | Always accepts int type as input, can return any type | public R apply(int i); |
LongFunction | Always accepts long type as input, can return any type | public R apply(long l); |
DoubleFunction | Always accepts double type as input, can return any type | public R apply(double d); |
ToIntFunction | Can take any type as input but always returns int type | public int applyAsInt(T t); |
ToLongFunction | Can take any type as input but always returns long type | public long applyAsLong(T t); |
ToDoubleFunction | Can take any type as input but always returns double type | public double applyAsDouble(T t); |
IntToLongFunction | Always takes int type as input and returns long type | public long applyAsLong(int i); |
IntToDoubleFunction | Always takes int type as input and return double type | public double applyAsDouble(int i); |
LongToIntFunction | Always takes long type as input and return int type | public int applyAsInt(long l); |
LongToDoubleFunction | Always takes long type as input and return double type | public double applyAsDouble(long l); |
DoubleToIntFunction | Always takes double type as input and return int type | public int applyAsInt(double d); |
DoubleToLongFunction | Always takes double type as input and return long type | public long applyAsLong(double d); |
ToIntBiFunction | Can take any types as two inputs but always returns int type | public int applyAsInt(T t, U u); |
ToLongBiFunction | Can take any types as two inputs but always returns long type | public long applyAsLong(T t, U u); |
ToDoubleBiFunction | Can take any types as two inputs but always returns double type | public double applyAsDouble(T t, U u); |
IntUnaryOperator | Input type and Output type both are int | public int applyAsInt(int i); |
LongUnaryOperator | Input type and Output type both are long | public long applyAsLong(long l); |
DoubleUnaryOperator | Input type and Output type both are double | public double applyAsDouble(double e); |
IntBinaryOperator | Both the Input types and a Output type are double | public int applyAsInt(int i, int j); |
LongBinaryOperator | Both the Input types and a Output type are double | public long applyAsLong(long l1, long l2); |
DoubleBinaryOperator | Both the Input types and a Output type are double | public double applyAsDouble(double d1, double d2); |
- IntConsumer
- LongConsumer
- DoubleConsumer
Primitive version of BiConsumer 4. ObjIntConsumer 5. ObjLongConsumer 6. ObjDoubleConsumer
Interface | Purpose | SAM |
---|---|---|
IntConsumer | Always accepts int type as a input | public void accept(int value); |
LongConsumer | Always accepts long type as a input | public void accept(long value); |
DoubleConsumer | Always accepts double type as a input | public void accept(double value); |
ObjIntConsumer | Always accepts two value as input, one of int type and another of any type | public void accept(T t, int value); |
ObjLongConsumer | Always accepts two value as input, one of long type and another of any type | public void accept(T t, long value); |
ObjDoubleConsumer | Always accepts two value as input, one of double type and another of any type | public void accept(T t, double value); |
- IntSupplier
- LongSupplier
- DoubleSupplier
- BooleanSupplier
Interface | Purpose | SAM |
---|---|---|
IntSupplier | Always supplies a int value | public int getAsInt(); |
LongSupplier | Always supplies a long value | public long getAsLong(); |
DoubleSupplier | Always supplies a double value | public double getAsDouble(); |
BooleanSupplier | Always supplies a boolean value | public boolean getAsBoolean(); |