Functional interfaces in Java

A functional interface is an interface that defines only one abstract method.

To precisely define an interface as functional, the @FunctionalInterface annotation has been added, working like the @Override principle. It will indicate the intent and prevent the second abstract method from being defined in the interface.

An interface can include as many default methods as you want and still remain functional, because default methods are not abstract.

Functional interfaces Function<T,R>, DoubleFunction<R>, IntFunction<R> and LongFunction<R>

Function<T,R> is an interface that implements a function that receives an instance of the T class as input and returns an instance of the R class at the output.

The default methods can be used to build call chains (compose, andThen).

Function<String, Integer> toInteger = Integer::valueOf;
Function<String, String> backToString = toInteger.andThen(String::valueOf);
backToString.apply("123");     // "123"

  • DoubleFunction<R> - a function that receives a Double as input and returns an instance of the R class at the output;
  • IntFunction<R> - a function that receives Integer as input and returns an instance of the R class at the output;
  • LongFunction<R> is a function that receives a Long input and returns an instance of the R class at the output.

Functional interfaces UnaryOperator<T>, DoubleUnaryOperator, IntUnaryOperator, and LongUnaryOperator

UnaryOperator<T> (unary operator) takes an object of type T as a parameter, performs operations on them, and returns the result of operations as an object of type T:

UnaryOperator<Integer> operator = x -> x * x;
System.out.println(operator.apply(5)); // 25

  • DoubleUnaryOperator - a unary operator that receives a Double as input;
  • IntUnaryOperator - a unary operator that receives an Integer as input;
  • LongUnaryOperator is a unary operator that takes Long as input.

Functional interfaces BinaryOperator<T>, DoubleBinaryOperator, IntBinaryOperator, and LongBinaryOperator

BinaryOperator<T> (binary operator) - an interface that implements a function that receives two instances of the T class as input and returns an instance of the T class at the output.

BinaryOperator<Integer> operator = (a, b) -> a + b;
System.out.println(operator.apply(1, 2)); // 3

  • DoubleBinaryOperator - a binary operator that receives Double as input;
  • IntBinaryOperator - a binary operator that receives Integer as input;
  • LongBinaryOperator is a binary operator that receives Long as input.

Functional interfaces Predicate<T>, DoublePredicate, IntPredicate, and LongPredicate

Predicate<T> (predicate) - an interface through which a function is implemented that receives an instance of the T class as input and returns a boolean value at the output.

The interface contains various default methods to build complex conditions (and, or, negate).

Predicate<String> predicate = (s) -> s.length() > 0;
predicate.test("foo"); // true
predicate.negate().test("foo"); // false

  • DoublePredicate - a predicate that receives a Double as input;
  • IntPredicate - a predicate that receives an Integer as input;
  • LongPredicate is a predicate that receives a Long input.

Functional interfaces Consumer<T>, DoubleConsumer, IntConsumer, and LongConsumer

Consumer<T> (consumer) - the interface through which a function is implemented that receives an instance of the class T as input, performs some action with it and returns nothing.

Consumer<String> hello = (name) -> System.out.println("Hello, " + name);
hello.accept("world");

  • DoubleConsumer - a consumer who receives a Double as input;
  • IntConsumer - a consumer that receives an Integer as input;
  • LongConsumer - a consumer that receives a Long input.

Supplier<T>, BooleanSupplier, DoubleSupplier, IntSupplier, and LongSupplier functional interfaces

Supplier<T> (supplier) - the interface through which a function is implemented that does not accept anything as input, but returns the result of the class T at the output;

Supplier<LocalDateTime> now = LocalDateTime::now;
now.get();

  • DoubleSupplier - supplier returning Double;
  • IntSupplier - supplier returning Integer;
  • LongSupplier - supplier returning Long.

BiConsumer<T,U> functional interface

BiConsumer<T,U> is an operation that takes two class arguments T and U does some action with them and returns nothing.

BiFunction<T,U,R> functional interface

BiFunction<T,U,R> is an operation that takes two class arguments T and U and returns the result of class R.

BiPredicate<T,U> functional interface

BiPredicate<T,U> is an operation that takes two class arguments T and U and returns a boolean result.

Functional interfaces like _To_Function

  • DoubleToIntFunction - an operation that takes an argument of the Double class and returns an Integer result;
  • DoubleToLongFunction - an operation that takes an argument of the Double class and returns a Long result;
  • IntToDoubleFunction - an operation that takes an argument of the Integer class and returns a result of type Double;
  • IntToLongFunction - an operation that takes an argument of the Integer class and returns a Long result;
  • LongToDoubleFunction - an operation that takes an argument of the Long class and returns a result of type Double;
  • LongToIntFunction is an operation that takes an argument of the Long class and returns an Integer result.

Functional interfaces ToDoubleBiFunction<T,U>, ToIntBiFunction<T,U> and ToLongBiFunction<T,U>

  • ToDoubleBiFunction<T,U> - an operation that takes two arguments of the T and U classes and returns a result of type Double;
  • ToLongBiFunction<T,U> - an operation that accepts two class arguments T and U and returns a Long type result;
  • ToIntBiFunction<T,U> - an operation that accepts two class arguments T and U and returns an Integer result.

Functional interfaces ToDoubleFunction<T>, ToIntFunction<T>, and ToLongFunction<T>

  • ToDoubleFunction<T> - an operation that takes an argument of class T and returns a result of type Double;
  • ToLongFunction<T> - an operation that takes an argument of class T and returns a result of type Long;
  • ToIntFunction<T> - an operation that takes an argument of class T and returns an Integer result.

Functional interfaces ObjDoubleConsumer<T>, ObjIntConsumer<T> and ObjLongConsumer<T>

  • ObjDoubleConsumer<T> - an operation that takes two arguments of the T and Double classes, performs some action with them and returns nothing;
  • ObjLongConsumer<T> - an operation that takes two arguments of the T and Long classes, performs some action with them and returns nothing;
  • ObjIntConsumer<T> is an operation that takes two arguments of the T and Integer classes, performs some action with them and returns nothing.

Read also:


Comments

Popular posts from this blog

ArrayList and LinkedList in Java, memory usage and speed

XML, well-formed XML and valid XML

Methods for reading XML in Java