Functional Interface is Single Abstract Method(SAM), so it used to target for a lambda expression or method reference.

A functional interface in Java is an interface that has exactly one abstract method. These interfaces can be used as the target type of a lambda expression or method reference.

Functional interfaces are typically used to represent a single function or action, and are often used as arguments to methods that perform operations on collections of data, such as filtering, mapping, or sorting.

Functional interfaces can be identified by the @FunctionalInterface annotation, which is optional but strongly recommended. If an interface is annotated with this annotation and has more than one abstract method, a compile-time error will be generated.

For example, the java.util.function package in Java 8 contains a number of functional interfaces, such as Predicate, Function, Consumer, and Supplier, which are used in various operations on collections and streams.

 Commonly Used Functional Interfaces

  • Function – it takes one argument and returns a result
  • Consumer – it takes one argument and returns no result (represents a side effect)
  • Supplier – it takes no arguments and returns a result
  • Predicate – it takes one argument and returns a boolean
  • BiFunction – it takes two arguments and returns a result
  • BinaryOperator – it is similar to a BiFunction, taking two arguments and returning a result. The two arguments and the result are all of the same types.
  • UnaryOperator – it is similar to a Function, taking a single argument and returning a result of the same type

Predicate Functional Interface

Predicate<T> is a functional interface that defines a single abstract method test(T t), which returns a boolean. It can be used to filter a stream of elements based on some criteria.

Predicate<Integer> isEven = x -> x % 2 == 0;
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
numbers.stream().filter(isEven).forEach(System.out::println);

Function Interface

Function<T, R> is a functional interface that defines a single abstract method apply(T t), which takes an argument of type T and returns an argument of type R. It can be used to apply a function to a stream of elements.

Function<Integer, Integer> square = x -> x * x;
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
numbers.stream().map(square).forEach(System.out::println);

Consumer Interface

Consumer<T> is a functional interface that defines a single abstract method accept(T t), which takes an argument of type T and returns no result. It can be used to perform some action on a stream of elements

Consumer<Integer> print = x -> System.out.println(x);
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
numbers.stream().forEach(print);

Supplier Interface

Supplier<T> is a functional interface that defines a single abstract method get(), which returns an object of type T. It can be used to generate a stream of elements.

Supplier<Integer> random = () -> (int) (Math.random() * 100);
Stream.generate(random).limit(10).forEach(System.out::println);

These functional interfaces and others like them provide a powerful and expressive way to perform operations on collections and streams in Java 8 and later, which can simplify code and make it more readable.