个人理解卵用没有
/**
*
* ->是叫箭头操作符
* -> 左边是型参列表
* -> 右边是lambda体,就是重写接口的方法体
*
* 接口里面只能有一个方法,两个方法就玩不转了
*/
@Test
public void test1(){
/**
* 无参无返回值
*/
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("我爱北京天安门");
}
};
runnable.run();
//改成lambda 表达式的写法
System.out.println("*******************");
// 表达式的本质是:作为接口的一个实例
Runnable runnable1 = () -> System.out.println("我爱北京故宫");
runnable1.run();
}
public interface Consumer<T> {
public void accept(T t);
}
/**
* 一个参数 没有返回值
*/
@Test
public void test2(){
System.out.println("====================");
Consumer<String> c =(s) -> {
System.out.println(s);
};
c.accept("ceshi lambda");
}
@Test
public void test5(){
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
};
System.out.println("*****************");
Comparator<Integer> com = (o3,o4)->{
System.out.println(o3);
System.out.println(o4);
return o3.compareTo(o4);
};
System.out.println(com.compare(12,34));
}
原文:https://www.cnblogs.com/gaohq/p/14725766.html