@[toc]

preface

One of the big highlights of Java 8 is the introduction of lambda expressions, which make code development much easier. It is used to define the method type interface that executes in a line, such as calling a method on an interface that you are too lazy to implement to write a lambda expression

What exactly does a lambda expression do? It is an anonymous implementation of the interface, followed by the arrow that implements the abstract method in the interface, and preceded by the arrow that passes the abstract method

Functional interface

The first thing we need to know about Java 8 lambda expressions is the functional interface. The term is used with Java 8 lambda expressions. Lambda expressions are anonymous implementations of functional interfaces

The JDK uses @functionalinterface at the top of the interface to indicate that this interface is a FunctionalInterface. A function is an interface that can receive a lambda expression as an anonymous implementation of the interface. For example, the map.foreach () method receives a BiConsumer functional interface, so lambda expressions can be written to implement the functional interface anonymously

In a normal interface, all methods must be abstract methods. In a functional interface, you can define not only static methods, but also a default-modified method called default void so that the function is an interface and you can simply new and call that method

The basic format

-> Indicates a parameter, followed by the return of a function expression

Expression is a generic expression. For example, it can be a+b. Without parentheses, the implemented method returns a+b

Statements can be a=1 but an error is reported if a+b is written, with parentheses to indicate that the full statement is returned

(parameters) -> expression (parameters) -> {statements; }Copy the code

A simple example

public class JustForTest {
	@Test
    public void test(a) {
        A addition = (a, b) -> a + b;
        System.out.println(addition.caozuo(1.2));
    }
    
    interface A {
        int sum(int x, int y); }}Copy the code

This example is straightforward, and is similar to an interface implementation class () that uses an anonymous interface (A addiction = new). This use of lambda expressions can save us a lot of code

scope

So let’s go straight to a couple of small examples

The sample a

Variables declared in lambda expressions may not be used outside the expression. It’s also easy to understand, because an expression is essentially implementing an anonymous inner class in which variables in methods cannot be used externally

The sample 2

As a class field variable, it can be referenced and modified in a method’s lambda expression. Look directly at variable in the following example

The sample of three

As a variable in a method, it cannot be modified in a lambda expression in a method, and errors can be detected before it is run. Red wavy line below. That is, the final property is implied

Map.forEach()

As mentioned earlier, the forEach() method receives a functional interface BiConsumer, so lambda expressions (anonymous implementations of the functional interface) can be used here.

Map<String, String> map = new HashMap<>();
map.put("a"."1");
map.put("b"."2");
map.forEach((key, value) -> {
    System.out.println(key + ":" + value);
});
Copy the code

The Stream of lambda

Streaming is also a nice new feature in Java 8. It is often used with lambda expressions. Collection classes call the stream() method of a stream and then call methods in the Stream interface, many of whose arguments are interfaces, to make lambda expressions.

Stream processing is not covered here, but is covered in another blog post: A new java8 feature called Stream processing

Double colon :: short lambda

The double colon :: is used to abbreviate lambda expressions to make them more concise. The abbreviation is technically η-conversion, pronounced eta-conversion. The double colon is preceded by the class name and followed by the method in the class. The whole thing indicates that an interface is implemented anonymously. The implementation of the method in the interface uses the method in the class

How do you understand that? The entire expression of the double colon is really an anonymous implementation class of an interface. After the double colon, what implemented method replaces the abstract method in the interface to be implemented. What class is specified before the double colon

For example, now let’s write A regular lambda expression for interface A

interface A {
    int sum(int a, int b);
}

public class JustForTest {
    @Test
    public void test1(a) { A a = (a, b) -> a + b; }}Copy the code

Let’s do that with a double colon

interface A {
    int sum(int a, int b);
}

public class JustForTest {
    @Test
    public void test1(a) { A a = Integer :: sum; }}Copy the code