新手网站建设,wordpress分类文章表格显示,电商企业网站建设,怎么制作网站站内链接目录
一、什么是方法引用
二、方法引用的规则
三、方法引用的分类#xff1a;
#xff08;一#xff09;、引用静态方法
#xff08;二#xff09;、引用成员方法
1、引用其他类的成员方法
2、引用本类的成员方法
3、引用父类的成员方法
#xff08;三#xff…目录
一、什么是方法引用
二、方法引用的规则
三、方法引用的分类
一、引用静态方法
二、引用成员方法
1、引用其他类的成员方法
2、引用本类的成员方法
3、引用父类的成员方法
三、引用构造方法
四、其他调用方式
1、使用类名引用成员方法
2、引用数组的构造方法 一、什么是方法引用 把已经有的方法拿过来当做函数式接口中抽象方法的方法体。 Java的方法引用是一种新的语法可以简化Lambda表达式的使用。方法引用可以将已有的方法作为Lambda表达式的替代进行传递。在Java中方法引用使用双冒号(::)操作符来表示。 二、方法引用的规则 引用处必须是函数式接口被引用的方法必须已经存在被引用方法的形参和返回值需要跟抽象方法保持一致被引用方法的功能要满足当前需求 三、方法引用的分类
一、引用静态方法 格式类名::静态方法 范例Integer::parseInt ArrayListString list new ArrayList();
Collections.addAll(list, 1, 2, 3, 4, 5);ListInteger collect list.stream().map(Integer::parseInt).collect(Collectors.toList());
System.out.println(collect);
二、引用成员方法 格式对象::成员方法 1、引用其他类的成员方法
格式其他类对象::成员方法 public static void main(String[] args) {ArrayListString list new ArrayList();Collections.addAll(list, 张无忌, 张强, 张三丰, 张翠山, 张良, 王二麻子, 谢广坤);list.stream().filter(new StringOperation()::stringJudge).forEach(s - System.out.println(s));}class StringOperation{public boolean stringJudge(String s){return s.startsWith(张) s.length() 3;}
}
2、引用本类的成员方法
格式this::成员方法 注如果是在静态方法中是没有this的只能用对象名 public static void main(String[] args) {ArrayListString list new ArrayList();Collections.addAll(list, 张无忌, 张强, 张三丰, 张翠山, 张良, 王二麻子, 谢广坤);ListString collect new Demo4().method(list);System.out.println(collect);
}public ListString method(ListString list){return list.stream().filter(this::stringJudge).collect(Collectors.toList());
}public boolean stringJudge(String s){return s.startsWith(张) s.length() 3;
}
3、引用父类的成员方法
格式super::成员方法 注如果是在静态方法中是没有super的只能用对象名 public class Demo5 {public static void main(String[] args) {ArrayListString list new ArrayList();Collections.addAll(list, 张无忌, 张强, 张三丰, 张翠山, 张良, 王二麻子, 谢广坤);ListString collect new Son().method(list);System.out.println(collect);}
}class Son extends Father {public ListString method(ListString list){return list.stream().filter(super::stringJudge).collect(Collectors.toList());}
}class Father{public boolean stringJudge(String s){return s.startsWith(张) s.length() 3;}
}三、引用构造方法 格式类名::new 范例Student::new public static void main(String[] args) {ArrayListString list new ArrayList();Collections.addAll(list, 张无忌,32, 张强,29, 张三丰,56, 张翠山,48, 张良,25, 王二麻子,28, 谢广坤,39);ListStudent collect list.stream().map(Student::new).collect(Collectors.toList());System.out.println(collect);
}public class Student {private String name;private int age;public Student() {}public Student(String s) {this.name s.split(,)[0];this.age Integer.parseInt(s.split(,)[1]);}public Student(String name, int age) {this.name name;this.age age;}…………
}
四、其他调用方式
1、使用类名引用成员方法 格式类名::成员方法 范例String::substring 特有规则 引用处必须是函数式接口被引用的方法必须已经存在被引用方法的形参需要跟抽象方法的第二个形参到最后一个形参保持一致返回值需要保持一致被引用方法的功能要满足当前需求 抽象方法形参的详解 第一个参数表示被引用方法的调用者决定了可以引用哪些类中的方法在stream 流当中第一个参数一般都表示流里面的每一个数据。假没流里面的数据是字符串那么使用这种方式进行方法引用只能引用 string 这个类中的方法第二个参数到最后一个参数跟被引用方法的形参保持一致如果没有第二个参数说明被引用的方法需要是无参的成员方法 局限性 不能引用所有类中的成员方法。是跟抽象方法的第一个参数有关这个参数是什么类型的那么就只能引用这个类中的方法。 ArrayListString list new ArrayList();
Collections.addAll(list, aaa, bbb, ccc, ddd);ListString collect list.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println(collect);
2、引用数组的构造方法 格式类名::new 范例Student::new ArrayListInteger list new ArrayList();
Collections.addAll(list, 1, 2, 3, 4, 5);Integer[] arr list.stream().toArray(Integer[]::new);
System.out.println(Arrays.toString(arr));