第一篇博客
斐波那契数列
http://www.nowcoder.com/questionTerminal/c6c7742f5ba7442aada113136ddea0c3
Lambda表达式
格式
Comparator<Integer> com = (o1,o2) -> Integer.compare(o1,o2);
(o1,o2):lambda形参列表 (其实就是接口中的抽象方法的形参列表)
->:箭头符号
Integer.compare(o1,o2) :lambda体 (其实就是重写的抽象方法的方法体)
举例一
未使用:
Runnable r1 = new Runnable() {
@Override
public void run() {
System.out.println("我爱北京***");
}
};
r1.run(); 使用后:
Runnable r2 = () -> System.out.println("我爱北京故宫");
r2.run();
} 结果相同
举例二
Comparator<Integer> com1 = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1,o2);
}
};
int compare1 = com1.compare(12,21); //-1 //Lambda表达式的写法
Comparator<Integer> com2 = (o1,o2) -> Integer.compare(o1,o2);
int compare2 = com2.compare(32,21); //1方法引用
//方法引用
Comparator<Integer> com3 = Integer :: compare;
int compare3 = com3.compare(32,21); //1函数式接口:什么时候用lambda表达式
实例化函数式接口
方法引用
京东公司氛围 301人发布