Java方法引用
方法引用
把已经有的方法拿过来用,当作函数式接口中抽象方法的方法体。
注意:
- 引用处必须是函数式接口
- 被引用的方法必须已经存在
- 被引用的方法的形参和返回值需要跟抽象方法保持一致
- 被引用的方法功能要满足当前需求
package com.faxont.function;
import java.util.Arrays;
import java.util.Comparator;
public class FunctionDemo1 {
public static void main(String[] args) {
// 需求:创建一个数组,进行倒序排列
Integer[] arr = {3, 5, 4, 1, 6, 2};
// 匿名内部类
// Arrays.sort(arr, new Comparator<Integer>() {
// @Override
// public int compare(Integer o1, Integer o2) {
// return o2 - o1;
// }
// });
// lambda 表达式
// Arrays.sort(arr, (Integer o1, Integer o2) -> {
// return o2 - o1;
// });
// lambda 表达式简化格式
// Arrays.sort(arr, (o1, o2) -> o2 - o1);
// 方法引用
Arrays.sort(arr, FunctionDemo1::subtraction);
System.out.println(Arrays.toString(arr));
}
public static int subtraction(int num1, int num2){
return num2 - num1;
}
}
方法引用的分类
引用静态方法
格式:类名::静态方法
练习:
集合中有以下数字,要求把题目都变成 int 类型
"1", "2", "3", "4", "5"
package com.faxont.function;
import java.util.ArrayList;
import java.util.Collections;
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");
// ArrayList<Integer> listInt = new ArrayList<>();
// for (String s : list) {
// int i = Integer.parseInt(s);
// listInt.add(i);
// }
// System.out.println(listInt);
list.stream()
.map(Integer::parseInt)
.forEach(System.out::println);
}
}
引用成员方法
格式:对象::成员方法
- 其他类:其他类对象::方法名
- 本类:this::方法名
- 父类:super::方法名
其中,2、3的引用处不能是静态方法
练习1:
集合中有一些名字,按照要求过滤数据
package com.faxont.function;
import java.util.ArrayList;
import java.util.Collections;
public class FunctionDemo3 {
public static void main(String[] args) {
/*
方法引用(引用成员方法)
格式:对象 :: 成员方法
1. 其他类:其他类对象 :: 方法名
2. 本类:this :: 方法名
3. 父类:super :: 方法名
需求:
集合中有一些名字,按照要求过滤数据
数据:"张无忌", "周芷若", "赵敏", "张强", "张三丰"
要求:只要以张开头,而且名字是3个字的
*/
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张强", "张三丰");
list.stream()
.filter(new StringOperation() ::stringJudge)
.forEach(System.out::println);
System.out.println("-----------------------");
// 静态方法中是没有 this 的
list.stream()
.filter(new FunctionDemo3() ::stringJudge)
.forEach(System.out::println);
}
public boolean stringJudge(String s){
return s.startsWith("张") && s.length() == 3;
}
}
package com.faxont.function;
public class StringOperation {
public boolean stringJudge(String s){
return s.startsWith("张") && s.length() == 3;
}
}
练习2:
GUI 界面中点击事件的方法引用写法
引用构造方法
格式:类名::new
练习:
集合里面存储姓名和年龄,比如:张无忌,15
要求:将数据封装成 Student 对象并收集到 List 集合中
package com.faxont.function;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class FunctionDemo4 {
public static void main(String[] args) {
/*
格式:类名 :: new
目的:创建这个类的对象
练习:
集合里面存储姓名和年龄,比如:张无忌,15
要求:将数据封装成 Student 对象并收集到 List 集合中
*/
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"张无忌,15", "周芷若,14", "赵敏,13", "张强,20", "张三丰,100", "张翠山,40", "张良,35", "王二麻子,37");
List<Student> students = list.stream()
.map(Student::new)
.collect(Collectors.toList());
System.out.println(students);
}
}
package com.faxont.function;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + "}";
}
public Student(String str){
String[] split = str.split(",");
this.name = split[0];
this.age = Integer.parseInt(split[1]);
}
}
其它调用方式
- 使用类名引用成员方法
格式:类名::成员方法
练习:
集合里面有一些字符串,要求变成大写后进行输出
package com.faxont.function;
import java.util.ArrayList;
import java.util.Collections;
public class FunctionDemo5 {
public static void main(String[] args) {
/*
方法引用(类名引用成员方法)
格式:类名::成员方法
练习:
集合里面有一些字符串,要求变成大写后进行输出
方法引用的规则:
1. 引用处必须是函数式接口
2. 被引用的方法必须已经存在
3. 被引用的方法的形参,需要跟抽象方法的第二个形参到最后一个形参保持一致,返回值需要保持一致
4. 被引用的方法功能要满足当前需求
抽象方法形参的详解:
第一个参数:表示被引用方法的调用者,决定了可以引用哪些类中的方法
在 Stream 流当中,第一个参数一般表示流里面的每一个数据
假设流里面的数据是字符串,那么使用这种方式进行方法引用,只能引用 String 这个类中的方法
第二个参数到最后一个参数:跟被引用方法的形参保持一致,如果没有第二个参数,说明被引用的方法需要是无参的成员方法
局限性:
不能引用所有类中的成员方法
是跟抽象方法的第一个参数有关,这个参数是什么类型的,那么就只能引用这个类中的方法
*/
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "aaa", "bbb", "ccc", "ddd");
// 拿着流里面的每一个数据,去调用 String 类中的 toUpperCase 方法,方法的返回值假设转换之后的结果
list.stream()
.map(String::toUpperCase)
.forEach(System.out::println);
}
}
- 引用数组的构造方法
格式:数据类型[]::new
练习:
集合中存储一些整数,收集到数组当中
package com.faxont.function;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
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));
}
}
方法引用综合练习
练习1:集合中存储一些字符串的数据,比如:张三,23.
收集到 Student 类型的数组当中(使用方法引用完成)
package com.faxont.practice;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Practice01 {
public static void main(String[] args) {
/*
练习1:集合中存储一些字符串的数据,比如:张三,23.
收集到 Student 类型的数组当中(使用方法引用完成)
*/
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "张无忌,15", "周芷若,14", "赵敏,13", "张强,20", "张三丰,100", "张翠山,40", "张良,35", "王二麻子,37");
Student[] students = list.stream()
.map(Student::new)
.toArray(Student[]::new);
System.out.println(Arrays.toString(students));
}
}
package com.faxont.practice;
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String str){
String[] split = str.split(",");
this.name = split[0];
this.age = Integer.parseInt(split[1]);
}
public String joinNameAge(){
return name + "-" + age;
}
@Override
public String toString() {
return "Student{name = " + name + ", age = " + age + "}";
}
public String getName() {
return name;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
练习2:创建集合添加学生对象,学生对象属性:name,age
只获取姓名并放到数组当中(使用方法引用完成)
package com.faxont.practice;
import java.util.ArrayList;
import java.util.Arrays;
public class Practice02 {
public static void main(String[] args) {
/*
练习2:创建集合添加学生对象,学生对象属性:name,age
只获取姓名并放到数组当中(使用方法引用完成)
*/
ArrayList<Student> list = new ArrayList<>();
list.add(new Student("张三", 23));
list.add(new Student("李四", 24));
list.add(new Student("王五", 25));
list.add(new Student("赵六", 26));
String[] strings = list.stream()
.map(Student::getName)
.toArray(String[]::new);
System.out.println(Arrays.toString(strings));
}
}
练习3:创建集合添加学生对象,学生对象属性:name,age
把姓名和年龄拼接成:张三-23的字符串,并放到数组当中(使用方法引用完成)
package com.faxont.practice;
import java.util.ArrayList;
import java.util.Arrays;
public class Practice03 {
public static void main(String[] args) {
/*
练习3:创建集合添加学生对象,学生对象属性:name,age
把姓名和年龄拼接成:张三-23的字符串,并放到数组当中(使用方法引用完成)
*/
ArrayList<Student> list = new ArrayList<>();
list.add(new Student("张三", 23));
list.add(new Student("李四", 24));
list.add(new Student("王五", 25));
list.add(new Student("赵六", 26));
String[] strings = list.stream()
.map(Student::joinNameAge)
.toArray(String[]::new);
System.out.println(Arrays.toString(strings));
}
}

浙公网安备 33010602011771号