方法引用
文章目录
把已经有的方法拿过来用,当做函数式接口中抽象方法的方法体。
::是什么符号
方法引用符
方法引用时需要注意什么
- 需要有函数式接口
- 被引用方法必须已经存在
- 被引用方法的形参和返回值需要跟抽象方法保持一致
- 被引用方法的功能要满足当前的需求
方法引用的分类
①引用静态方法
格式:类名::静态方法
范例:Integer::parseInt
练习:
集合中有以下数字,要求把它们都变成int类型
“1”,“2”,“3”,“4”,“5”
package com.example.demo;
import java.util.ArrayList;
import java.util.Collections;
public class FunctionDemo1 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"1","2","3","4","5");
list.stream().map(Integer::parseInt).forEach(s-> System.out.println(s));
}
}
②引用成员方法
格式:对象::成员方法
引用其他类的成员方法
格式:其他类对象::方法名
引用本类的成员方法(引用处不能是静态方法)
this::方法名
引用父类的成员方法(引用处不能是静态方法)
super::方法名
练习
1.集合中有一些名字,按照要求过滤数据

package com.example.demo;
import java.util.ArrayList;
import java.util.Collections;
public class FunctionDemo2 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"张无忌","周芷若","赵敏","张强","张三丰");
list.stream().filter(new StringOperation()::stringJugde).forEach(s-> System.out.println(s));
}
}
③引用构造方法
格式:类名::new
范例:Student::new
练习
集合里面存储姓名和年龄,比如:张无忌,15
要求:将数据封装成Student对象并收集到List集合中
package com.example.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class FunctionDemo3 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"张无忌,15","周芷若,14","赵敏,13","张强.20","张三丰,100");
// list.stream().map(new Function<String, Student>() {
// public Student apply(String s){
// String[] arr = s.split(",");
// String name = arr[0];
// int age = Integer.parseInt(arr[1]);
// return new Student(name,age);
// }
// }).collect(Collectors.toList());
List<Student> newList = list.stream().map(Student::new).collect(Collectors.toList());
System.out.println(newList);
}
}
④其他调用方法
使用类名引用成员方法
格式:类名::成员方法
范例:String::subString
练习
集合里面一些字符串,要求变成大写后进行输出

package com.example.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.function.Function;
public class FunctionDemo4 {
public static void main(String[] args) {
// 1.创建集合对象
ArrayList<String> list = new ArrayList<>();
// 2.添加数据
Collections.addAll(list,"aaa","bbb","ccc");
// 3.变成大写后进行输出
//map(String::toUpperCase)
//拿着流里面的每一个数据,去调用String类中的toUpperCase方法,方法的返回值就是转换之后的结果
list.stream().map(String::toUpperCase).forEach(s-> System.out.println(s));
/*list.stream().map(new Function<String, String>() {
public String apply(String s){
return s.toUpperCase();
}
}).forEach(s-> System.out.println(s));*/
}
}
引用数组的构造方法
格式:数据类型[]::new
范例:int[]::new
练习:
集合中存储一些整数,收集到数组当中
package com.example.demo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class FunctionDemo5 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Collections.addAll(list,1,2,3,4,5);
//数组的类型,需要跟流中数据的类型保持一致、
/*Integer[] array = list.stream().toArray(new IntFunction<Integer[]>() {
public Integer[] apply(int value) {
return new Integer[value];
}
});*/
Integer[] arr1 = list.stream().toArray(Integer[]::new);
System.out.println(Arrays.toString(arr1));
}
}

被折叠的 条评论
为什么被折叠?



