One of the important new features in JDK8 is Lambda expressions:


Lambda expressions, also known as closures, are the most important new feature driving the Release of Java 8. Lambda expressions are essentially anonymous methods that allow functions to be passed as arguments to a method or code as data. Lambda expressions can make code much more compact.

Syntax structure of a Lambda expression:

(Parameter list) -> {code block}

Note:

· Argument types can be omitted and the compiler can infer for itself · Parentheses can be omitted if there is only one argument · code blocks can be omitted if there is only one line of code · Return can be omitted if the code block is a single line and is a resulting expression

(param1,param2) ->{} (type1 param1,type2 param2) ->{body} the left side of the arrow is the parameter of the abstract method and the right side is the implementation body of the abstract methodCopy the code

1) A Lambda expression can have zero or more arguments. 2) All arguments must be enclosed in parentheses, separated by commas. 3) The parentheses indicate that the parameter set is empty. For example, the type of the () -> 42.4) argument can be either explicitly declared or inferred from the context.

Functional interface:


An interface that contains only one abstract method is called a functional interface.

Runnable and Comparator are examples of functional interfaces. In practice, functional interfaces are so fragile that whenever someone adds one more method to an interface, it is no longer a functional interface and will fail to compile. Java8 provides a special annotation @functionalinterface to overcome the vulnerability mentioned above and explicitly indicate the function interface.

1,Function type interface:

@functionalInterface public interface Function<T,R> {// accept a parameter T, return a result R R apply(T T); }Copy the code

Function stands for Function that takes arguments and returns values.

2, the Consumer series:

@functionalInterface public interface Consumer<T> {// Accept (T T); }Copy the code

The Consumer series, like the Function series, has various derived interfaces but does not return any results.

3. Predicate Series:

@functionalInterface public interface Predicate<T> {// Accept T type parameters, return Boolean type result Boolean test(T T); }Copy the code

There are four core functional interfaces built into Java:

  • Consumer: A Consumer interface that represents an operation that takes a single input parameter and returns no result. Apply the operation to an object of type T. Void accept(T T)
  • Supplier: Supplier interface, similar to a Supplier, returns an object of type T. Interface method: T get()
  • Function

    : functional interface, representing a Function that takes one argument and produces a result. R apply(T T)
    ,>
  • Predicate: An interface that determines whether an object of type T satisfies a constraint and returns Boolean. Boolean test(T T)

# # # StreamAPI:


There are two most important changes in Java8. The first is a Lambda expression; The other is the Stream API.

The new Stream API(java.util.stream) brings functional programming for the generated environment to Java libraries. This is one of the biggest improvements to the Java library so far, allowing developers to write more efficient, concise, and compact code.

What exactly is a Stream?

Is a data channel used to manipulate sequences of elements generated by data sources (collections, arrays, and so on).

Stream operates in three steps:

  1. Create Stream: A data source (e.g., collection, array) and get a Stream.
  2. Intermediate operations: A chain of intermediate operations that perform a series of processes on the data source.
  3. Termination operation (terminal operation) : A termination operation that performs an intermediate chain of operations and produces a result.

Note:

  1. The Stream itself does not store elements.
  2. Stream does not change the source object. Instead, they return a new Stream holding the result.
  3. The Stream operation executes lazily. This also means they wait until they need results.