public class LambdaTest { @Test public void test1(){ Runnable r1 = new Runnable() { @Override public void run() { System.out.println("coding"); } }; r1.run(); System.out.println("***********************"); Runnable r2 = () -> System.out.println("coding"); r2.run(); } @Test public void test2(){ //@FunctionalInterface 直接实现 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); System.out.println(compare1); System.out.println("***********************"); //@FunctionalInterface 保留参数和方法体 Comparator<Integer> com2 = (o1,o2) -> Integer.compare(o1,o2); int compare2 = com2.compare(32,21); System.out.println(compare2); System.out.println("***********************"); //@FunctionalInterface 只保留方法体 Comparator<Integer> com3 = Integer :: compare; int compare3 = com3.compare(32,21); System.out.println(compare3); } }
java8 lamda函数进化史
最新推荐文章于 2023-07-11 21:30:00 发布