lambda表达式

2021-05-21  本文已影响0人  安然在路上

前言

package com.lagou.rpc.api;

import java.util.Comparator;
import java.util.TreeSet;

public class lambda {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("run");
            }
        };
        new Thread(runnable).start();


        Runnable runnable2 = () -> System.out.println("hello.lambda");
        new Thread(runnable2).start();

        new Thread(()-> System.out.println("hello,lambda!!!")).start();


        Comparator<String> comparator = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length()-o2.length();
            }
        };
        TreeSet<String> treeSet = new TreeSet<>(comparator);
    }

    //Comparator是函数式接口,因为equals是Object的方法
    Comparator<String> comparator2 = (o1,o2)-> o1.length()-o2.length();
    TreeSet<String> treeSet2 = new TreeSet<>(comparator2);
    TreeSet<String> treeSet3 = new TreeSet<>((o1,o2)-> o1.length()-o2.length());


}

上一篇下一篇

猜你喜欢

热点阅读