JDK8_Lambda 表达式的使用1

package com.zhou.java1;

import org.junit.jupiter.api.Test;

import java.util.Comparator;

/**
 * Lambda 表达式的使用举例
 *
 * @author upzhou
 * @create 2022-04-03 16:12
 */
public class LambdaTest {

    @Test
    public void test1(){

        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("我爱北京天安门");
            }
        };

        r1.run();

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

        Runnable r2 = () -> System.out.println("我爱北京故宫");

        r2.run();
    }

    @Test
    public void test2(){

        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1,o2);
            }
        };

        int compare1 = com1.compare(12, 22);
        System.out.println(compare1);

        System.out.println("**************");
        //Lambda 表达式的写法:
        Comparator<Integer> com2 = (o1,o2) -> Integer.compare(o1,o2);

        int compare2 = com2.compare(32, 22);
        System.out.println(compare2);

        System.out.println("**************");
        //方法引用
        Comparator<Integer> com3 = Integer::compare;

        int compare3 = com3.compare(32, 22);
        System.out.println(compare3);


    }
}

 

posted @ 2022-04-30 15:27  UpZhou  阅读(19)  评论(0)    收藏  举报