Java8新特性_Lambda 基础语法

从匿名类到 Lambda 的转换

从原来使用匿名内部类作为参数传递到Lambda表达式作为参数传递

Lambda 表达式语法
Lambda 表达式在Java 语言中引入了一个新的语法元素和操作符。这个操作符为 “->” , 该操作符被称
为 Lambda 操作符或箭头操作符。它将 Lambda 分为两个部分:
左侧:指定了 Lambda 表达式需要的所有参数
右侧:指定了 Lambda 体,即 Lambda 表达式要执行的功能。

类型推断
上述 Lambda 表达式中的参数类型都是由编译器推断得出的。Lambda 表达式中无需指定类型,程序依然可以编译,这是因为 javac 根据程序的上下文,在后台推断出了参数的类型。Lambda 表达式的类型依赖于上下文环境,是由编译器推断出来的。这就是所谓的“类型推断”

2-函数式接口
什么是函数式接口

只包含一个抽象方法的接口,称为函数式接口。
你可以通过 Lambda 表达式来创建该接口的对象。(若 Lambda 表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法上进行声明)。
我们可以在任意函数式接口上使用 @FunctionalInterface 注解,这样做可以检查它是否是一个函数式接口,同时 javadoc 也会包含一条声明,说明这个接口是一个函数式接口。
自定义函数式接口

package day01.com.lm.java8;

import org.junit.Test;

import java.util.*;
import java.util.function.Consumer;

/**
 * 一、Lambda表达式的基础语法:Java8中引入了一个新的操作符“->” 该操作符称为箭头操作符或Lambda操作符
 *                             箭头操作符将Lambda表达式拆分成两部分:
 * 左侧:Lambda表达式的参数列表
 * 右侧:Lambda表达式中所需要执行的功能,即Lambda体
 *
 * 语法格式一:无参数,无返回值
 *      () -> System.out.println("Hello Lambda!");
 *
 * 语法格式二:有一个参数,并且无返回值
 *      (x) -> System.out.println(x);
 *
 * 语法格式三:只有一个参数,小括号可以省略不写
 *      x -> System.out.println(x);
 *
 * 语法格式四:有两个以上的参数,有返回值,并且Lambda体有多条语句
 *        Comparator<Integer> com = (x, y) -> {
 *             System.out.println("函数式接口");
 *             return Integer.compare(x, y);
 *         };
 *
 * 语法格式五:若Lambda体中只有一条语句,return和大括号都可以省略不写
 *      Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
 *
 * 语法格式六:Lambda表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出数据类型,即“类型推断”
 *      Comparator<Integer> com = (Integer x, Integer y) -> Integer.compare(x, y);
 *
 * 上联:左右遇一括号省
 * 下联:左侧推断类型省
 * 横批:能省则省
 *
 * 二、Lambda表达式需要“函数式接口”的支持
 * 函数式接口:接口中只有一个抽象方法的接口,称为函数式接口。
 *             可以使用注解@FunctionalInterface修饰可以检查是否是函数式接口
 */
public class TestLambda2 {
    
    

    @Test
    public void test1() {
    
    //无参数,无返回值
        int num = 0;//jdk1.7前,必须加final

        Runnable r = new Runnable() {
    
    
            @Override
            public void run() {
    
    
                System.out.println("Hello World!" + num);
            }
        };

        r.run();

        System.out.println("--------------------------------------------");

        Runnable r1 = () -> System.out.println("Hello World!" + num);
        r1.run();
    }

    @Test
    public void test2() {
    
    //有一个参数,并且无返回值
        Consumer<String> con = (x) -> System.out.println(x);
        con.accept("民哥威武!");
        //只有一个参数,小括号可以省略不写
        Consumer<String> con2 = x -> System.out.println(x);
        con2.accept("民哥威武!");
    }

    @Test
    public void test3() {
    
    //有两个以上的参数,有返回值,并且Lambda体有多条语句
        Comparator<Integer> com = (x, y) -> {
    
    
            System.out.println("函数式接口");
            return Integer.compare(x, y);
        };
    }

    @Test
    public void test4() {
    
    //若Lambda体中只有一条语句,return和大括号都可以省略不写
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
        Comparator<Integer> com2 = (Integer x, Integer y) -> Integer.compare(x, y);
    }

    @Test
    public void test5() {
    
    
        String[] strs = {
    
    "aaa", "bbb", "ccc"};
//        String[] strs;
//        strs = {"aaa", "bbb", "ccc"};

        List<String> list = new ArrayList<>();

        show(new HashMap<>());
    }

    public void show(Map<String, Integer> map) {
    
    

    }

    //需求:对一个数进行运算
    @Test
    public void test6() {
    
    
        Integer num = operation(100, (x) -> x * x);
        System.out.println(num);

        System.out.println(operation(200, (y) -> y + 200));
    }

    public Integer operation(Integer num, MyFun mf) {
    
    
        return mf.getValue(num);
    }
}
package day01.com.lm.java8;

@FunctionalInterface
public interface MyFun<T> {
    
    

    public Integer getValue(Integer num);
}

猜你喜欢

转载自blog.csdn.net/m0_51945027/article/details/119962244