Java - Functional Interface

1 - About

Functional Programming - Functional Interface in Java.

A functional interface is is a interface that contains only one abstract method.

The interface is so simple that java defines several standard (built-in) functional interfaces

3 - Usage

Functional interfaces represent abstract concepts like:

4 - Structure

A functional interface is is a interface that contains only one abstract method.

The single abstract method is called the functional method.

It may contain one or more:

4.1 - Annotation

The FunctionalInterface annotation is an aid to capture design intent not a requirement. Compilers may generate an error.

4.2 - Method

The single abstract method in a function interface is called the functional method.

5 - Creation

Implementation of functional interfaces can be created with:

6 - Standard

The predefined/standard/built-in function interface can find in the package java.util.function package

They are generic interface.

All standard interface follows the following naming convention.

  • Supplier (nilary function to R).
  • Predicate (unary function from T to boolean), abstract method that returns a boolean (Example: filtering)

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

  • Consumer (unary function from T to void), abstract method that returns no value (Example: print)

interface Consumer<T> {
    void accept(T t);
}

  • Function (unary function from T to R), - abstract method that returns a value (Example: retrieve information or validation)

interface Function<T> {
   R apply(T t);
}

  • UnaryOperator (unary function from T to T), - abstract method that return always returns its input argument (handy for chaining)

interface UnaryOperator<T> {
   T apply(T t);
}


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap