Java 8 – Pre-defined Functional interface

In this article, we will discuss pre-defined Functional interface in Java 1.8 version.

There are around 40 pre-defined Funtional Interface in Java 1.8 version under java.util.function package. We will categorize each pre-defined Functional Interface on the basis no. of input/output argument or data-types.

1. One argument Pre-defined Functional Interface:

1.1 Predicate

  • This pre-defined Functional Interface accepts input argument of any data-type and perform conditional checks and always return Boolean value true/false
  • Method signature: boolean test(T t);

1.2 Function

  • This pre-defined Functional Interface accepts input argument of any data-type and perform some operations and return result in any data-type
  • Method signature: R apply(T t);

1.3 Consumer

  • This pre-defined Functional Interface accepts input argument of any data-type and perform some operations and doesn’t return any result
  • Method signature: void accept(T t);

1.4 Supplier

  • This pre-defined Functional Interface doesn’t accepts any input argument but performs some operations and return result in any data-type
  • Method signature: T get();

2. Two argument Pre-defined Functional Interface:

2.1 BiPredicate

  • This pre-defined Functional Interface accepts 2 input arguments of any data-type and perform conditional checks and always return Boolean value true/false
  • This is exactly same as Predicate, except that number of arguments BiPredicate accepts is 2 whereas Predicate accepts is just 1
  • Method signature: boolean test(T t, U u);

2.2 BiFunction

  • This pre-defined Functional Interface accepts 2 input arguments of any data-type and perform some operations and return result in any data-type
  • This is exactly same as Function, except that number of arguments BiFunction accepts is 2 whereas Function accepts is just 1
  • Method signature: R apply(T t, U u);

2.3 BiConsumer

  • This pre-defined Functional Interface accepts input of any data-type and perform some operations and doesn’t return any result
  • This is exactly same as Consumer except that, no. of arguments BiConsumer accepts is 2 whereas Consumer accepts is just 1
  • Method signature: void accept(T t, U u);

3. Pre-defined primitive Predicate Functional Interface:

3.1 IntPredicate

  • This pre-defined Predicate primitive Functional Interface accepts input argument of primitive-type int only and perform some conditional checks and always return Boolean value true/false
  • Method signature: boolean test(int value);

3.2 LongPredicate

  • This pre-defined Predicate primitive Functional Interface accepts input argument of primitive-type long only and perform some conditional checks and always return Boolean value true/false
  • Method signature: boolean test(long value);

3.3 DoublePredicate

  • This pre-defined Predicate primitive Functional Interface accepts input argument of primitive-type double and perform some conditional checks and always return Boolean value true/false
  • Method signature: boolean test(double value);

4. Pre-defined primitive Function Functional Interface:

4.1 IntFunction

  • This pre-defined Function primitive Functional Interface accepts input argument of primitive-type int and perform some operations and return result in any data-type
  • Method signature: R apply(int value);

4.2 LongFunction

  • This pre-defined Function primitive Functional Interface accepts input argument of primitive-type long and perform some operations and return result in any data-type
  • Method signature: R apply(long value);

4.3 DoubleFunction

  • This pre-defined Function primitive Functional Interface accepts input argument of primitive-type double and perform some operations and return result in any data-type
  • Method signature: R apply(double value);

4.4 ToIntFunction

  • This pre-defined Function primitive Functional Interface accepts input argument of any data-type and perform some operations and return result in primitive-type int only
  • Method signature: int applyAsInt(T value);

4.5 ToLongFunction

  • This pre-defined Function primitive Functional Interface accepts input argument of any data-type and perform some operations and return result in primitive-type long only
  • Method signature: long applyAsLong(T value);

4.6 ToDoubleFunction

  • This pre-defined Function primitive Functional Interface accepts input argument of any data-type and perform some operations and return result in primitive-type double only
  • Method signature: double applyAsDouble(T value);

4.7 IntToLongFunction

  • This pre-defined Function primitive Functional Interface accepts input argument of primitive-type int only and perform some operations and return result in primitive-type long only
  • Method signature: long applyAsLong(int value);

4.8 IntToDoubleFunction

  • This pre-defined Function primitive Functional Interface accepts input argument of primitive-type int only and perform some operations and return result in primitive-type double only
  • Method signature: double applyAsDouble(int value);

4.9 LongToIntFunction

  • This pre-defined Function primitive Functional Interface accepts input argument of primitive-type long only and perform some operations and return result in primitive-type int only
  • Method signature: int applyAsInt(long value);

4.10 LongToDoubleFunction

  • This pre-defined Function primitive Functional Interface accepts input argument of primitive-type long only and perform some operations and return result in primitive-type double only
  • Method signature: double applyAsDouble(long value);

4.11 DoubleToIntFunction

  • This pre-defined Function primitive Functional Interface accepts input argument of primitive-type double only and perform some operations and return result in primitive-type int only
  • Method signature: int applyAsInt(double value);

4.12 DoubleToLongFunction

  • This pre-defined Function primitive Functional Interface accepts input argument of primitive-type double only and perform some operations and return result in primitive-type long only
  • Method signature: long applyAsLong(double value);

5. Pre-defined primitive BiFunction Functional Interface:

5.1 ToIntBiFunction

  • This pre-defined Function primitive Functional Interface accepts 2 input arguments of any data-type and perform some operations and return result in primitive-type int only
  • Method signature: int applyAsInt(T value);

5.2 ToLongBiFunction

  • This pre-defined Function primitive Functional Interface accepts 2 input arguments of any data-type and perform some operations and return result in primitive-type long only
  • Method signature: long applyAsLong(T t, U u);

5.3 ToDoubleBiFunction

  • This pre-defined Function primitive Functional Interface accepts 2 input arguments of any data-type and perform some operations and return result in primitive-type double only
  • Method signature: double applyAsDouble(T t, U u);

6. Pre-defined primitive Consumer Functional Interface:

6.1 IntConsumer

  • This pre-defined Consumer primitive Functional Interface accepts input argument of primitive-type int only and perform some operations and doesn’t return any result
  • Method signature: void accept(int value);

6.2 LongConsumer

  • This pre-defined Consumer primitive Functional Interface accepts input argument of primitive-type long only and perform some operations and doesn’t return any result
  • Method signature: void accept(long value);

6.3 DoubleConsumer

  • This pre-defined Consumer primitive Functional Interface accepts input argument of primitive-type double only and perform some operations and doesn’t return any result
  • Method signature: void accept(double value);

6.4 ObjIntConsumer

  • This pre-defined Consumer primitive Functional Interface accepts 2 input arguments of Object type & primitive-type int only and perform some operations and doesn’t return any result
  • Method signature: void accept(T t, int value);

6.5 ObjLongConsumer

  • This pre-defined Consumer primitive Functional Interface accepts 2 input arguments of Object type & primitive-type long only and perform some operations and doesn’t return any result
  • Method signature: void accept(T t, long value);

6.6 ObjDoubleConsumer

  • This pre-defined Consumer primitive Functional Interface accepts 2 input arguments of Object type & primitive-type double only and perform some operations and doesn’t return any result
  • Method signature: void accept(T t, double value);

7. Pre-defined primitive Supplier Functional Interface:

7.1 IntSupplier

  • This pre-defined Supplier primitive Functional Interface doesn’t accepts any input argument but perform some operations and return result in primitive-type int only
  • Method signature: int getAsInt();

7.2 LongSupplier

  • This pre-defined Supplier primitive Functional Interface doesn’t accepts any input argument but perform some operations and return result in primitive-type long only
  • Method signature: long getAsLong();

7.3 DoubleSupplier

  • This pre-defined Supplier primitive Functional Interface doesn’t accepts any input argument but perform some operations and return result in primitive-type double only
  • Method signature: void accept(double value);

7.4 BooleanSupplier

  • This pre-defined Supplier primitive Functional Interface doesn’t accepts any input argument but perform some operations and return result in primitive-type boolean only
  • Method signature: boolean getAsBoolean();

8. UnaryOperator and its primitive Functional Interface:

8.1 UnaryOperator

  • When both input & output arguments are of same type, then instead of going for Function, use UnaryOperator Functional Interface
  • This is basically performance improvised/optimized version of java.util.function.Function interface
  • Also UnaryOperator interface extends Function interface

8.2 IntUnaryOperator

  • When both input & output arguments are of primitive-type int, then use UnaryOperator<Integer> Functional Interface which has method applyAsInt();
  • Method signature: int applyAsInt(int operand);

8.3 LongUnaryOperator

  • When both input & output arguments are of primitive-type long, then use UnaryOperator<Long> Functional Interface which has method applyAsLong();
  • Method signature: long applyAsLong(long operand);

8.4 DoubleUnaryOperator

  • When both input & output arguments are of primitive-type double, then use UnaryOperator<Double> Functional Interface which has method applyAsDouble();
  • Method signature: double applyAsDouble(double operand);

9. BinaryOperator and its primitive Functional Interface:

9.1 BinaryOperator

  • When there are 2 input arguments & output argument are of same type, then instead of going for BiFunction, use BinaryOperator Functional Interface
  • This is basically performance improvised/optimized version of java.util.function.BiFunction interface
  • And BinaryOperator interface extends BiFunction interface

9.2 IntBinaryOperator

  • When there are 2 input arguments & output arguments are of primitive-type int, then use BinaryOperator<Integer> Functional Interface which has method applyAsInt();
  • Method signature: int applyAsInt(int left, int right);

9.3 LongBinaryOperator

  • When there are 2 input arguments & output arguments are of primitive-type long, then use BinaryOperator<Long> Functional Interface which has method applyAsLong();
  • Method signature: long applyAsLong(long left, long right);

9.4 DoubleBinaryOperator

  • When there are 2 input arguments & output arguments are of primitive-type double, then use BinaryOperator<Double> Functional Interface which has method applyAsDouble();
  • Method signature: double applyAsDouble(double left, double right);

References:

Happy Coding !!
Happy Learning !!

Java 8 - Predicate Functional Interface
Java 8 - default and static methods