October 24, 2022

Built-in Functional Interfaces in Java

Post about Functional Interface in Java gives an overview of functional interfaces and how Lambda expressions can be used to implement functional interfaces. At the same time it doesn’t mean whenever you want to use lambda expression you will have to write your own custom functional interface. In Java there are many built-in functional interfaces which cover many different contexts when used with lambda expressions in Java.

Built-in functional interfaces Java package

In Java there is a package java.util.function that defines many general purpose functional interfaces used by the JDK and used by user code as well.

Functional interfaces in this package can be categorized into five types-

  1. Consumer
  2. Function
  3. Predicate
  4. Supplier
  5. Operators that extend Function

Consumer functional interface

java.util.function.Consumer<T> functional interface represents an operation that accepts a single input argument and returns no result. Definition of the Consumer is as given below which consists of an abstract method accept() and a default method andThen()-

@FunctionalInterface
public interface Consumer<T> {
  void accept(T t);

  default Consumer<T> andThen(Consumer<? super T> after) {
    Objects.requireNonNull(after);
    return (T t) -> { accept(t); after.accept(t); };
  }
}

Here is a list of other pre-defined Consumer functional interfaces sharing the same behavior of consuming the passed value(s) and returning no result.

  • BiConsumer<T,U>- Represents an operation that accepts two input arguments and returns no result.
  • DoubleConsumer- Represents an operation that accepts a single double-valued argument and returns no result.
  • IntConsumer- Represents an operation that accepts a single int-valued argument and returns no result.
  • LongConsumer- Represents an operation that accepts a single long-valued argument and returns no result.
  • ObjDoubleConsumer<T>- Represents an operation that accepts an object-valued and a double-valued argument, and returns no result.
  • ObjIntConsumer<T>- Represents an operation that accepts an object-valued and a int-valued argument, and returns no result.
  • ObjLongConsumer<T>- Represents an operation that accepts an object-valued and a long-valued argument, and returns no result.

See post Java Consumer Functional Interface Examples for examples of Consumer interface and BiConsumer Functional Interface Java Examples for examples of BiConsumer interface

Supplier functional interface

java.util.function.Supplier<T> functional interface represents a function that supplies a result. Supplier interface definition is as given below-

@FunctionalInterface
public interface Supplier<T> {
  T get();
}

Here is a list of pre-defined Supplier functional interfaces sharing the same behavior of supplying a result.

  • BooleanSupplier- Represents a supplier of boolean-valued results.
  • DoubleSupplier- Represents a supplier of double-valued results.
  • IntSupplier- Represents a supplier of int-valued results.
  • LongSupplier- Represents a supplier of long-valued results.

Function functional interface

java.util.function Function<T,R> functional interface represents a function that accepts one argument and produces a result.

Abstract method in this functional interface is apply(Object).

@FunctionalInterface
public interface Function<T, R> {
  R apply(T t);
}

Here is a list of other pre-defined Function functional interfaces sharing the same behavior of accepting argument(s) and producing a result.

  • BiFunction<T,U,R>- Represents a function that accepts two arguments and produces a result.
  • DoubleFunction<R>- Represents a function that accepts a double-valued argument and produces a result.
  • DoubleToIntFunction- Represents a function that accepts a double-valued argument and produces an int-valued result.
  • DoubleToLongFunction- Represents a function that accepts a double-valued argument and produces a long-valued result.
  • IntFunction<R>- Represents a function that accepts an int-valued argument and produces a result.
  • IntToDoubleFunction- Represents a function that accepts an int-valued argument and produces a double-valued result.
  • IntToLongFunction- Represents a function that accepts an int-valued argument and produces a long-valued result.
  • LongFunction<R>- Represents a function that accepts a long-valued argument and produces a result.
  • LongToDoubleFunction- Represents a function that accepts a long-valued argument and produces a double-valued result.
  • LongToIntFunction- Represents a function that accepts a long-valued argument and produces an int-valued result.
  • ToDoubleBiFunction<T,?U>- Represents a function that accepts two arguments and produces a double-valued result.
  • ToDoubleFunction<T>- Represents a function that produces a double-valued result.
  • ToIntBiFunction<T,?U>- Represents a function that accepts two arguments and produces an int-valued result.
  • ToIntFunction<T>- Represents a function that produces an int-valued result.
  • ToLongBiFunction<T,?U>- Represents a function that accepts two arguments and produces a long-valued result.
  • ToLongFunction<T>- Represents a function that produces a long-valued result.

Operator functional interfaces

Function functional interfaces may produce result of the same type as argument or different but there are Operator functional interfaces that always return the value of same type as the passed arguments. Base Operator functional interfaces extend their Function interface counterpart like UnaryOperator extends Function and BinaryOperator extends BiFunction.

  • BinaryOperator<T>- Represents an operation upon two operands of the same type, producing a result of the same type as the operands.
  • DoubleBinaryOperator- Represents an operation upon two double-valued operands and producing a double-valued result.
  • DoubleUnaryOperator- Represents an operation on a single double-valued operand that produces a double-valued result.
  • IntBinaryOperator- Represents an operation upon two int-valued operands and producing an int-valued result.
  • IntUnaryOperator- Represents an operation on a single int-valued operand that produces an int-valued result.
  • LongBinaryOperator- Represents an operation upon two long-valued operands and producing a long-valued result.
  • LongUnaryOperator- Represents an operation on a single long-valued operand that produces a long-valued result.
  • UnaryOperator<T>- Represents an operation on a single operand that produces a result of the same type as its operand.

Predicate Functional interface

java.util.function.Predicate<T> functional interface represents a boolean valued function returning either true or false. Abstract method in this functional interface is test(Object).

@FunctionalInterface
public interface Predicate<T> {
  boolean test(T t);
}

Here is a list of pre-defined Predicate functional interfaces sharing the same behavior of accepting argument(s) and producing a boolean result.

  1. BiPredicate<T,?U>- Represents a predicate (boolean-valued function) of two arguments.
  2. DoublePredicate- Represents a predicate (boolean-valued function) of one double-valued argument.
  3. IntPredicate- Represents a predicate (boolean-valued function) of one int-valued argument.
  4. LongPredicate- Represents a predicate (boolean-valued function) of one long-valued argument.

That's all for the topic Built-in Functional Interfaces in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment