public void test1(){
Runnable r1 = new Runnable(){
@Override
public void run(){
System.out.prinln("我爱你");
}
};
r1.run();
System.out.println("-----");
//Lambda表达式写法
Runnable r2 = () ->{
System.out.prinln("我爱你");
};
r2.run();
}
public void test2(){ Comparator<Integer> com1 = new Comparator<Integer>(){ public int compare(Integer o1,Integer o2){ return Integer.compare(o1,o2); } }; int compare1 = com1.compare(12,21); System.out.println(compare1); //Lambda表达式写法 Comparator<Integer> com2 =(o1, o2) -> Integer.compare(o1,o2); int compare2 = com2.compare(45,33); System.out.println(compare2); //-> Lambda操作符或箭头操作符,左边是Lambda形参列表,对应着要重写的接口中的抽象方法的形参列表 右边是Lambda体,对应着接口的实现类要重写的方法的方法体 //方法引用 Comparator<Integer> com3 = Integer::compare; int compare3 = com3.compare(12,21); System.out.println(compare3); }