zoukankan      html  css  js  c++  java
  • Java8-Lambda方法引用

    Lambda方法的引用可以参考 https://www.cnblogs.com/happyflyingpig/p/9004534.html 中的示例三,接下来讨论一下构造函数的方法引用

     java8给我们提供了 Supplier<T> 、Function<T,R>、BiFunction<T,U,R>等函数式接口(就是interface中只有一个抽象函数的接口),我们可以利用这几个函数式接口来创建构造函数的方法引用。

    一、先准备一个类,这个类有无惨构造函数、一个参数的构造函数、两个参数多的构造函数

    public class Apple {
        private int weight;
        private String color;
    
        public Apple(){}
    
        public Apple(int weight) {
            this.weight = weight;
        }
    
        public Apple(int weight, String color) {
            this.weight = weight;
            this.color = color;
        }
    
       setters();&getters();&toString();
    }

    二、当构造函数是无参数的时候

    /**
    * 假设构造函数没有参数 它适合Supplier签名
    */
    Supplier<Apple> supplier1 = Apple::new; // 构造函数引用指向默认的Apple()构造函数

    supplier1.get(); //调用get方法将产生一个新的Apple
    //这两个等价
    Supplier<Apple> supplier2 = () -> new Apple();
    supplier2.get();

    三、当构造函数是有一个参数的时候

    /**
     * 如果构造函数签名是Apple(Integer weight), 也就是构造函数只有一个参数, 那么他就适合Function接口的签名
     */
     Function<Integer, Apple> function1 = Apple::new; //指向Apple(Integer weight)的构造函数引用
     Apple apple1 = function1.apply(150); //调用该Function函数的apply方法,产生一个Apple
     //等价于
     Function<Integer, Apple> function2 = (weight) -> new Apple(weight);
     Apple apple2 = function2.apply(200);

    简单的一个应用

    List<Integer> integerList = Arrays.asList(4, 5, 6, 7, 8);
    getAppleList(integerList, Apple::new);
    
    public static List<Apple> getAppleList(List<Integer> list, Function<Integer, Apple> function) {
        List<Apple> result = new ArrayList<>();
        for (Integer it : list) {
            Apple apple = function.apply(it);
            result.add(apple);
        }
        return result;
    }

    四、当构造函数是有两个参数的时候

    /**
     * 如果构造函数签名是Apple(Integer weibht, String color), 也就是构造函数有两个参数,那么就适合BiFunction接口的签名
     */
    BiFunction<Integer, String, Apple> biFunction1 = Apple::new;
    biFunction1.apply(100, "红色");
    //等价于
    BiFunction<Integer, String, Apple> biFunction2 = (weight, color) -> new Apple(weight, color);
    biFunction2.apply(100, "红色");

    简单应用

    /**
     * 应用,根据上面学习的例子,我们可以不用实例化来引用它
     */
    Map<String, Function<Integer, Fruit>> fruitMaps = new HashMap<>(20);
    fruitMaps.put("banner", Banner::new);
    fruitMaps.put("pear", Pear::new);
    
    //获取水果对象
    Fruit banner = fruitMaps.get("banner").apply(10);
    Fruit pear = fruitMaps.get("pear").apply(20);
    
    System.out.println(banner);
    System.out.println(pear);

    参考:

    【1】《Java8实战》

  • 相关阅读:
    Hihocoder 1275 扫地机器人 计算几何
    CodeForces 771C Bear and Tree Jumps 树形DP
    CodeForces 778D Parquet Re-laying 构造
    CodeForces 785E Anton and Permutation 分块
    CodeForces 785D Anton and School
    CodeForces 785C Anton and Fairy Tale 二分
    Hexo Next 接入 google AdSense 广告
    如何统计 Hexo 网站的访问地区和IP
    Design and Implementation of Global Path Planning System for Unmanned Surface Vehicle among Multiple Task Points
    通过ODBC接口访问人大金仓数据库
  • 原文地址:https://www.cnblogs.com/happyflyingpig/p/9079320.html
Copyright © 2011-2022 走看看