运行后会输出hello
helloProcess finished with exit code 0对象方法引入这是一种更为方便的写法 ,  我记得在mybatis-plus使用自带条件查询的时候会用到这种方式 , 以下用简单例子来实现 。java8中提供了public interface Function<T, R>的类 , 这个类也是使用@FunctionalInterface注解 , 可见也是函数式接口 。代码:
// 在car中声明一个方法public class Car {    public String Info() {        return "保时捷 - 帕拉梅拉";    }}// 函数式接口@FunctionalInterfacepublic interface CarInFoService {    String getCar(Car car);}测试采用了三种方式进行比较
public class LambdaTest4 {    public static void main(String[] args) {        System.out.println("**************匿名内部类**************");        CarInFoService carInFoService = new CarInFoService() {            @Override            public String getCar(Car car) {                return car.Info();            }        };        System.out.println(carInFoService.getCar(new Car()));        System.out.println("**************lambda**************");        CarInFoService carInFoService2 = (car) -> car.Info();        System.out.println(carInFoService2.getCar(new Car()));        System.out.println("**************对象方法引入**************");        CarInFoService carInFoService3 = Car::Info;        System.out.println(carInFoService3.getCar(new Car()));        // R apply(T t); T  apply方法传递的参数类型 : R apply 方法返回的类型        Function<String, Integer> function = String::length;        System.out.println(function.apply("lyd_code"));    }}结果

文章插图
Lambda表达式遍历通过foreach循环遍历 , forEach实际上需要使用匿名内部类Consumer<? super E> 。

文章插图
代码如下
public class LambdaTest5 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("lyd"); list.add("tom"); list.add("jack"); list.forEach(new Consumer<String>() { @Override public void accept(String s) { System.out.println("name: " + s); } }); System.out.println("lambda表达式"); /** * s:遍历链表所得到的元素字符串 */ list.forEach(s -> System.out.println("name: " + s)); }}
经验总结扩展阅读
- 圆葱饼如何制作
- 字符串 P7361 「JZOI-1」拜神
- 甩手功的十大功效真有效吗
- 芒果的最佳搭配水果
- 审计中注册会计师的责任是什么
- 分式方程无解是什么意思
- 竹笋怎样保存时间更久
- 形容心情好的句子
- 眼罩哪里可以买到
- 小苏打去斑效果好吗

 
   
   
   
   
   
   
   
   
   
   
   
  