The new functional interfaces in Java 8 are a surprising feature. Java is an object-oriented language, where objects are first-class citizens and functions cannot be taken as arguments to another function. The functional interface provides the ability to take a function as a parameter and participate in the execution of another function as a parameter, so that the function has more flexibility.

Pure functions

Pure functions have two properties: immutable objects and no side effects. Immutable objects and no side effects guarantee the stability of the performance of the function

  • Immutable object: in addition toparameterIs used for function executionData (object)It’s fixed

Immutable objects ensure that the result of a function execution is the same when the parameters are the same. For example, if the function y = x + 1 and the parameter x is constant, the result y does not change. If the constant 1 is replaced by a database query, that is, y = x + db(x), then the function y = x + db(x) is not immutable because the result of the db(x) query is not fixed

  • No side effectThe: function does not modify external data or objects

No side effects are useful in concurrent programming because external objects are not modified and can be used without considering unexpected results when multiple threads are executed in an unfixed order

Function side effects: Additional effects that occur when a function is called. For example, modify global variables or modify parameters

Lambda expressions

Lambda expressions mean anonymous functions, that is, Lambda expressions are generated as functions, not classes

Lambda is structured as (argument) -> {}, where -> represents passed execution and {} represents the body of the function (the execution logic of the function), for example:

Function<Integer, Integer> add = (Integer x) -> {
    return x + 1;
};

Function<Integer, Integer> add1 = (Integer x) -> x + 1;
Copy the code
  • When only one parameter is used, no parentheses are required(a)
  • If there are no arguments, parentheses must be used(a)Represents an empty parameter list
  • For multiple arguments, put the argument list in parentheses(a)
  • When the body of a function is only one line, you may not write return

An important function of Lambda is to take the implementation of the object to be anonymous, for example:

Thread thread = new Thread(new Runnable() {
    @Override
    public void run(a) {
        System.out.println("hello"); }}); Thread thread1 =new Thread(() -> System.out.println("hello"));
Copy the code

Method references

A method reference represents a function and defines a function in the form of a class name or instance name :: method name, for example;

public void test(a) {

    Function<Integer, Integer> add = this::add;
}

private Integer add(int x){
    return x + 1;
}
Copy the code

Functional interface

Interfaces in Java 8 that satisfy the requirement to contain only one abstract method are called functional interfaces. The interface is usually declared to be functional using the @functionalInterface annotation. Such as:

@FunctionalInterface
public interface AddFunction<T.R> {

    Integer add(T t, R r);
}
Copy the code

injava.util.functionThe system in the package provides some basicFunctional interface, as shown below:The most common ones areFunction<T, R>,Supplier<T>,Consumer<T>,Predicate<T>

Function<T, R>

Function<T, R>: represents a function that takes an argument and returns a result. Which is to receive aTType, returns oneRThe function definition is as follows:Any function that fits this form can be viewed asFunction<T, R>The implementation class, calledapplyMethod executes the function, as shown below:

Function

: compose and andThen
,>

  • compose: Perform the parameter operation first and then the original operation

composeReceive receive oneFunction<T, R>As an argument, and returns a combined new function. The result of this argument (Function) is executed as an argument to the original FunctionExample:andThen: Perform the original operation first and then the parameter operationandThenAlso receive receive oneFunction<T, R>Returns a new combined function as an argument. But here’s the differenceandThenThe two functions are executed sequentially, that is, the original Function is executed and the result of the original Function is taken as an argument (Function)Example:

Supplier<T>

Supplier<T>: represents a function that returns results without arguments.Supplier“Producer/provider” means producer/provider, often used where some default value is provided or data is provided in a default way:

Consumer<T>

Consumer<T>: a function that takes a single input parameter and does not return a result.Consumer“Consumer” means consumer, usually where an operation is performed on data and no value is required to be returned.ConsumerThe function definition ofExample:Consumer<T>There is also a combinatorial function inandThen, represents the consumption parameters in order. The function definition is as follows:Example:

Predicate<T>

Predicate<T>: represents a function that takes a single argument and returns a Boolean value.PredicateThe function is defined as follows:Predicate<T>The combinatorial function of isand,or,negateSaid,With or notLogical example:

Currie,

Will amulti-parameterFunction of, converts toA series of single parametersFunction is calledThe function is curialized. This is done by converting individual arguments to functions, and the final conversion between arguments to the relationship between arguments and functions, for example:

BiFunction

: indicates the function with two parameters that have return values
,>

The add function in the figure above takes two arguments and is curlified by converting the second argument and the return value to a new function as the return value of the first argument