前言:
- lambda是结合函数式接口使用的
- 函数式接口是指只有一个抽象方法的接口
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());
}
网友评论