上海网站改版方案,国家信用信息公示系统山东,杭州建设职业学校官方网站,wordpress 关闭摘要如果不熟悉Java8新特性的小伙伴#xff0c;初次看到函数式接口写出的代码可能会是一种懵逼的状态#xff0c;我是谁#xff0c;我在哪#xff0c;我可能学了假的Java#xff0c;(・∀・(・∀・(・∀・*)#xff0c;但是语言都是在进步的#xff0c;就好比面向对象的语言…如果不熟悉Java8新特性的小伙伴初次看到函数式接口写出的代码可能会是一种懵逼的状态我是谁我在哪我可能学了假的Java(・∀・(・∀・(・∀・*)但是语言都是在进步的就好比面向对象的语言Java也可以写出优雅的函数式调用学习的过程并不复杂当你学会了Java8中函数式编程的新特性你一定会对他爱不释手的。下面介绍一下基于Lambda表达式简写的两种引用。避免再次看到这种代码时的尴尬。方法引用方法引用一般包含下面三种写法传统的写法我们可能都是通过对象.去调用实例方法或使用类.调用静态方法但是学完方法引用后就可以可以使用这三种方式去调用方法但是要符合一定的规则。对象::实例方法/*** 对象调用实例方法*/public static void objMethod(){List list new ArrayList ();list.add(1);list.add(2);list.add(3);list.forEach((i)-{PrintStream out System.out;Consumer consumer out::println;consumer.accept(i);});list.forEach(System.out::println);}最常用的System.out.println类::实例方法/*** 判断两个字符串是否相同** param str1* param str2* return*/public static boolean isEqual(String str1, String str2) {BiPredicate b (s1,s2)-s1.equals(str2); ①BiPredicate bp String::equals;return bp.test(str1, str2);}类::静态方法/*** 比较大小* param x* param y* return*/public static boolean compareValue(int x, int y){Comparator compare Integer::compare; ②return compare.compare(x, y) 0;}其实不管是哪一种调用方式都是有规律可循的这里总结一下在使用Lambda表达式的过程中符合什么样的规则才可以使用方法引用的模式去写。Lambda体中调用方法的参数列表与返回值类型要与函数式接口中抽象方法的函数列表和返回值类型保持一致 Integer::compare ②Lambda参数列表中的第一参数是实例方法的调用者而第二个参数是实例方法的参数时 可以使用ClassName::method ①构造方法引用简称花式new对象一个简单的new对象也要写的高端、大气、上档次既可以掌握新知识又可以ZB赶紧学习吧。ClassName::new资源类public class Apple {private String color;private double weight;public Apple(){}public Apple(String color) {this.color color;}public Apple(double weight) {this.weight weight;}public Apple(String color, double weight) {this.color color;this.weight weight;}public String getColor() {return color;}public void setColor(String color) {this.color color;}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight weight;}Overridepublic String toString() {return Apple{ color color \ , weight weight };}}测试代码public static void main(String[] args) {//无参构造//Supplier supplier () - new Apple(); Lambda表达式写法Supplier supplier Apple::new;Apple apple supplier.get();System.out.println(NoArgsConstructor: apple);//有参构造//Function function (x) - new Apple(x); Lambda表达式写法// 构造引用Function function Apple::new;Apple apply function.apply(1.0);System.out.println(OneArgsConstructor: apply);BiFunction bf Apple::new;Apple bi bf.apply(Red, 2.0);System.out.println(TwoArgsConstructor: bi);}输出结果NoArgsConstructor: Apple{colornull, weight0.0}OneArgsConstructor: Apple{colornull, weight1.0}TwoArgsConstructor: Apple{colorRed, weight2.0}当构造方法无参时使用Supplier有一个参数时使用Function两个参数时使用BiFunction。这里很容易得出一个规律当使用构造方法引用时函数式接口的参数列表需要和构造方法的参数列表保持一致。我们也可以用这些函数式接口改写传统的创建数组的方式初始化一个指定长度的数组比如Function fun String[]::new;String[] strArr fun.apply(10);也可以这样写public static T[] initArray(int num, Function function){return function.apply(num);}调用Apple[] strings initArray(10, x - new Apple[x]);System.out.println(strings.length);疑惑根据传入的参数返回指定的对象数组引用不过这样还不如直接创建。不知道读者有没有考虑这里为什么不可以用一个泛型来new那样就可以创建一个通用数组引用但是Java中的泛型是伪泛型在编译器就会进行泛型擦除所以不能通过new关键字来创建一个泛型对象具体内容可以在查阅其他资料了解泛型以及泛型擦除的原理这里不做深究。如果有其他写法期待你的回复。