Java基础_方法引用
方法引用概述
- 方法引用:把已经存在的方法拿过来用,当作函数式接口中抽象方法的方法体
- “::”:方法引用符
- 方法引用时需要注意
- 引用处需要是函数式接口
- 被引用的方法需要已经存在
- 被引用方法的形参和返回值需要跟抽象方法的形参和返回值保持一致
- 被引用方法的功能需要满足当前的需求
- 方法引用格式
- 引用静态方法
- 类名::静态方法
- 引用成员方法
- 对象::成员方法
- this::成员方法
- super::成员方法
- 引用构造方法
- 类名::new
- 使用类名引用成员方法
- 类名::成员方法
- 引用数组的构造方法
- 数据类型[]::new
- 引用静态方法
public class FunctionDemo1 {
public static void main(String[] args) {
Integer[] nums = {5, 2, 3, 4, 1, 6};
// 匿名内部类
// Arrays.sort(nums, new Comparator<Integer>() {
// @Override
// public int compare(Integer o1, Integer o2) {
// return o2-o1;
// }
// });
// lambda表达式
// 因为第二个参数的类型Comparator是一个函数式接口
// Arrays.sort(nums, (Integer o1, Integer o2) -> {
// return o2-o1;
// });
// lambda表达式简化
// Arrays.sort(nums, (o1, o2) -> o2-o1);
// 方法引用
Arrays.sort(nums, FunctionDemo1::subtraction);
System.out.println(Arrays.toString(nums));
}
public static int subtraction(int n1, int n2){
return n2-n1;
}
}
方法引用的分类
引用静态方法
public class FunctionDemo2 {
public static void main(String[] args) {
/**
* 集合中有以下数字,要求把他们都变成int类型
* “1”“2”“3”“4”“5”
*/
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"1", "2", "3", "4", "5");
List<Integer> res = list.stream().map(Integer::parseInt).collect(Collectors.toList());
System.out.println(res);
}
}
引用成员方法
public class FunctionDemo3 {
public static void main(String[] args) {
/**
* 方法引用(引用成员方法)
* 格式
* 其他类:其他类对象::方法名
* 本类:this::方法名
* 父类:super::方法名
*
* 需求:
* 集合中有一些名字,按照要求过滤数据数据:"张无忌","周芷若","赵敏""张强”"张三丰
* 要求:只要以张开头,而且名字是3个字的
*/
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "张无忌","周芷若","赵敏","张强","张三丰");
list.stream()
.filter(new StringOperation()::stringJudge)
.forEach(s -> System.out.println(s));
// 静态方法中没有this
list.stream()
.filter(new FunctionDemo3()::stringJudge)
.forEach(s -> System.out.println(s));
}
public boolean stringJudge(String s){
return s.startsWith("张")&&s.length()==3;
}
}
引用构造方法
public class FunctionDemo4 {
public static void main(String[] args) {
/**
* 方法引用(引用构造方法)
* 格式
* 类名::new
* 目的:
* 创建这个类的对象
* 需求:
* 集合里面存储姓名和年龄,要求封装成student对象并收集到List集合中
*/
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "张无忌-18", "周芷若-19", "赵敏-21", "张强-23", "张三丰-27");
List<Student> res = list.stream().map(Student::new).collect(Collectors.toList());
System.out.println(res);
}
}
public class Student {
private String name;
private int age;
public Student(String str) {
String[] arr = str.split("-");
this.name = arr[0];
this.age = Integer.parseInt(arr[1]);
}
}
其他调用方式
使用类名引用成员方法
public class FunctionDemo5 {
public static void main(String[] args) {
/**
* 方法引用(类名引用成员方法)
* 格式
* 类名::成员方法
* 需求:
* 集合里面一些字符串,要求变成大写后进行输出
*/
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "a", "b", "c", "d", "e");
/**
* 此类方法引用的特殊规则
* 1. 被引用方法的形参,需要跟抽象方法的第二个形参到最后一个形参保持一致,返回值需要保持一致
* 2. 不能引用所有类中的成员方法,只能引用抽像方法第一个参数的类型的方法。
*
* 抽像方法形参详解
* 第一个参数:表示被引用方法的调用者,决定了可以引用哪些类中的方法
* 在Stream流当中,第一个参数一般都表示流里面的每一个数据
* 假设流里面的数据是字符串,那么使用这种方式进行方法引用,只能引用String这个类中的方法
*
* 第二个参数到最后一个参数:跟被引用方法的形参保持一致,如果没有第二个参数,说明被引用方法需要是无参的成员方法
*/
// 将流里面的每一个数据,去调用String类中的toUpperCase方法,方法的返回值就是转换之后的结果
list.stream().map(String::toUpperCase).forEach(s -> System.out.println(s));
}
}
引用数组的构造方法
public class FunctionDemo6 {
public static void main(String[] args) {
/**
* 方法引用(数组的构造方法)
* 格式
* 数据类型[]::new
* 目的:
* 创建一个指定类型的数组
* 需求:
* 集合中存储一些整数,收集到数组当中
*/
ArrayList<Integer> list = new ArrayList<>();
Collections.addAll(list, 1, 2, 3, 4, 5);
Integer[] integers = list.stream().toArray(Integer[]::new);
System.out.println(Arrays.toString(integers));
}
}
练习
转成自定义对象并收集到数组
public class FunctionDemo7 {
public static void main(String[] args) {
/**
* 需求:
* 集合中存储一些字符串的数据,
* 比如:张三,23
* 收集到student类型的数组当中
*/
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "张无忌-18", "周芷若-19", "赵敏-21", "张强-23", "张三丰-27");
Student[] students = list.stream().map(Student::new).toArray(Student[]::new);
System.out.println(Arrays.toString(students));
}
}
获取部分属性并收集到数组
public class FunctionDemo8 {
public static void main(String[] args) {
/**
* 需求:
* 创建集合添加学生对象
* 学生对象属性:name,age
* 要求:
* 获取姓名并放到数组当中
* 使用方法引用完成
*/
ArrayList<Student> list = new ArrayList<>();
Collections.addAll(list,
new Student("zhangsan-23"),
new Student("lisi-24"),
new Student("wangwu-25")
);
String[] names = list.stream().map(Student::getName).toArray(String[]::new);
System.out.println(Arrays.toString(names));
}
}
将对象转为字符串并收集到数组
public class FunctionDemo9 {
public static void main(String[] args) {
/**
* 需求:
* 创建集合添加学生对象
* 学生对象属性:name,age
* 要求:
* 把姓名和年龄拼按成:张三-23的字符串,并放到数组当中
* 使用方法引用完成
*/
ArrayList<Student> list = new ArrayList<>();
Collections.addAll(list,
new Student("zhangsan-23"),
new Student("lisi-24"),
new Student("wangwu-25")
);
String[] strs = list.stream()
.map(student -> student.getName() + "-" + student.getAge())
.toArray(String[]::new);
System.out.println(Arrays.toString(strs));
}
}
来源
黑马程序员. 阿玮Java零基础
Gitee地址
https://gitee.com/yu-ba-ba-ba/awJava