java8新特性

Lambda函数

1、内置的四大核心函数式接口

Consumer<T>    消费型接口

  void  accept(T t);

Supplier<T>  供给型接口

  T get();

Function(R,T)  函数型接口

  R  apply(T t);

Predicate<T>  断言型接口

  boolean  test(T t);

2、Lambda方法引用:若Lambda体,需要实现的功能已经有方法提供了实现,可以使用方法引用

  2.1、方法引用的格式

  2.1.1、对象名::实例方法名

  2.1.2、类名::静态方法名

  2.1.3、类名::实例方法名

3、方法引用规则:

  3.1、方法引用的方法的参数列表和返回值类型 必须和函数式接口中  抽象方法的参数列表和返回值类型  保持一致

         //PrintStream的方法
        void println(Object x);
        //Consumer的抽象方法
        void accept(T t);    

  3.1.1、对象名::方法引用如下

        //1、消费型函数
        //未使用方法引用的lambda写法
        PrintStream ps =System.out;
        Consumer con1 = (s)->ps.println(s);
        con1.accept("未使用方法引用的lambda写法前");
        System.out.println("----------------------------------------");
        //使用方法引用的lambda写法
        Consumer con2 = ps::println;
        con2.accept("使用方法引用的lambda写法后");
        //2、供给型函数
        Order order = new Order();
        //未使用对象方法引用之前
        order.setShopNm("门店名称");
        Supplier sup1 = ()->order.getShopNm();
        System.out.println(sup1.get());
        //使用对象方法引用之后
        Supplier sup2 = order::getShopNm;
        sup2.get();
        System.out.println(sup2.get());

  3.1.2、类名::静态方法引用如下       

        //2、使用类名::方法名
        //使用类名::方法名之前
        Comparator<Integer> com1 = (a,b)->Integer.compare(a,b);
        System.out.println(com1.compare(1, 2));
        //使用类名::方法名之后
        Comparator<Integer> com2 = Integer::compare;
        System.out.println(com2.compare(1, 2));

  3.1.3、类名::实例方法名引用如下

      3.1.4、返回值相同的情况下,函数式接口中抽象方法参数列表的第一个参数,是引用方法的调用者,第二个参数(或无参)是方法引用方法的参数时,可以使用  类名::实例方法名

        //3、使用实例名::方法名(两个参数)
        //使用实例名::方法名之前
        BiPredicate<String,String> bp1 = (a,b) -> a.equals(b);
        System.out.println(bp1.test("abc", "bcd"));
        //使用实例名::方法名之后
        BiPredicate<String,String> bp2 = String::equals;
        System.out.println(bp2.test("bcd", "bcd"));

        //使用实例名::方法名(第二个参数为无参)
        //使用实例名::方法名之前
        Order order = new Order();
        order.setShopNm("门店");
        Function<Order,String> fu1 = (e)->e.getShopNm();
        System.out.println(fu1.apply(order));
        System.out.println("--------------------------");
        //使用实例名::方法名之后
        Function<Order,String> fu2 = Order::getShopNm;
        System.out.println(fu2.apply(order));    

 4、构造器引用(规则:函数式接口中抽象方法的参数列表与构造器的参数列表一致时)

  4.1、格式   类名::new

        //使用类名::new 
        //未使用类名::new之前(无参构造)
        Supplier<Order> sup1 = ()->new Order();
        System.out.println(sup1);
        //使用类名::new之后(无参构造)
        Supplier<Order> sup2 = Order::new;
        System.out.println(sup2);

        //未使用类名::new之前(有参构造)
        BiFunction<Long,String,Order> bf1 = (a, b)->new Order(a, b);
        System.out.println(bf1.apply(1l, "1234"));
        //使用类名::new之后(有参构造)
        BiFunction<Long,String,Order> bf2 = Order::new;
        System.out.println(bf2.apply(2l, "2345"));

 5、数组引用

  5.1、格式  类名[]::new

      
   //使用数组引用之前 Function<Integer,String[]> fu1 = (s)->new String[s]; System.out.println(fu1.apply(5).length); //使用数组引用之后 Function<Integer,String[]> fu2 = String[]::new; System.out.println(fu2.apply(6).length);

 

 

   

  

 

posted @ 2022-03-27 22:18  西风51668  阅读(66)  评论(0)    收藏  举报