New features of JDK8 - LambdaExpress


foreword

LambdaExpress was added in JDK8 and has been widely used by major manufacturers so far. It has strong advantages: faster speed; less code; powerful streaming API; easy to parallelize.


1. What is LambdaExpress?

  • basic concept
    1. Essentially it is an anonymous method, i.e. a function without a function name.
    2. It is named based on the lambda calculus in mathematics.
    3. Its core idea is to turn object-oriented transfer data into transfer behavior.
    4. It can represent a closure (here to be distinguished from a closure in the traditional sense of mathematics).
  • rule
    1. Must be a functional interface.
    2. There can be only one method in an interface.

2. How to use

1. Use formulas

  • copy parentheses
  • hard right arrow
  • floor braces

2. Code example

1) No return value, no parameters:

Interface: Person

package com.juc.study.iServer;
/**
 * 接口 —— 人
 */
public interface Person {
    
    

    /**
     * 只有这一个说的方法
     */
    public void say();
}

Test code:

import com.juc.study.iServer.Person;
import org.junit.Test;

public class TestJucStudy {
    
    

    @Test
    public void testLambdaExpress() {
    
    
        Person p = () -> {
    
    System.out.println("你好!!!");};
        p.say();
    }
}

Results of the:

你好!!!

2) There are parameters and return values

Interface: Person

package com.juc.study.iServer;

/**
 * 接口 —— 人
 */
public interface Person {
    
    

    /**
     * 两个学科的总成绩
     * @param i
     * @param y
     * @return
     */
    public int totalScore(int i, int y);
}

Test code:

import com.juc.study.iServer.Person;
import org.junit.Test;

public class TestJucStudy {
    
    
    @Test
    public void testLambdaExpress() {
    
    
        Person p = (int x, int y) -> {
    
    return x + y;};
        System.out.println(p.totalScore(90, 99));
    }
}

Results of the:

189

3. How to ensure that the interface is a functional interface

  • Add the annotation @FunctionalInterface on the interface.
  • If there is only one method in the interface, JDK8 will automatically add @FunctionalInterface to this interface, commonly known as implicit definition.
  • If there is more than one method in the interface, then JDK8 will consider it a common standard interface.
  • If you want to force it to become a functional interface, you can add the @FunctionalInterface annotation to the interface. When multiple methods are added to the interface, an error will be reported.
  • JDK8 allows the implementation of some methods, the code is as follows:

Interface: Person

package com.juc.study.iServer;

/**
 * 接口 —— 人
 */
@FunctionalInterface
public interface Person {
    
    

    /**
     * 两个学科的总成绩
     * @param i
     * @param y
     * @return
     */
    public int totalScore(int i, int y);

    /**
     * 使用default关键字可以在接口中实现方法,也不会影响函数式接口的定义
     */
    public default void say() {
    
    
        System.out.println("Hello JAVA 8");
    }
    
	/**
     * 静态方法
     */
    public static void attr() {
    
    
        System.out.println("基本属性");
    }
}

Test code:

import com.juc.study.iServer.Person;
import org.junit.Test;

public class TestJucStudy {
    
    

    @Test
    public void testLambdaExpress() {
    
    
        Person p = (int x, int y) -> {
    
    return x + y;};
        System.out.println(p.totalScore(90, 99));
        p.say();
        Person.attr();
    }
}

Results of the:

189
Hello JAVA 8
基本属性

使用default关键字可以在接口中实现方法,也不会影响函数式接口的定义,并且可以有多个; 也可以定义多个static 方法 。


3. Summary

The above content is the basic knowledge and usage of Lambda expressions. Interested students can continue to study in depth.

Guess you like

Origin blog.csdn.net/yezhijing/article/details/128203599