方法引用:就是把已经有的方法拿过来用,当作函数式接口抽象方法的方法体。
条件:
- 引用处必须是函数式接口
- 被引用的方法必须已经存在
- 被引用方法的形参和返回值需要和抽象方法保持一致
- 被引用的方法要满足当前需求
格式:类名::方法名
引例
public class Demo01 { public static int sub(int a, int b) { return a - b; } public static void main(String[] args) { Integer[] arr = {3, 5, 4, 1, 6, 2}; Arrays.sort(arr, (o1, o2) -> o1 - o2); System.out.println(Arrays.toString(arr)); Arrays.sort(arr, Demo01::sub); } }
|
引用静态方法
格式:类名::静态方法
public class Demo02 { public static void main(String[] args) { List<String> list = new ArrayList<>(); Collections.addAll(list, "1", "2", "3", "4", "5"); List<Integer> list1 = list.stream().map(Integer::parseInt) .collect(Collectors.toList()); } }
|
引用成员方法
格式:对象::成员方法
- 其他类:其它类对象::方法名
- 本类:this::方法名
- 父类:super::方法名
public class Demo03 { public boolean StringJudge(String s) { return s.startsWith("张") && s.length() == 3; } public static void main(String[] args) { List<String> list = new ArrayList<>(); Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤"); List<String> list1 = list.stream().filter(new Demo03()::StringJudge) .collect(Collectors.toList()); } }
|
注:引用本类成员方法、父类成员方法处不能是静态方法
引用构造方法
格式:类名::new
class Student { private String name; private Integer no; Student(String str) { String[] split = str.split("-"); this.name = split[0]; this.no = Integer.valueOf(split[1]); } } public class Demo04 { public static void main(String[] args) { List<String> list = new ArrayList<>(); Collections.addAll(list, "张无忌-1", "周芷若-2", "赵敏-3", "张三丰-4", "张翠山-5", "张良-6", "王二麻子-7", "谢广坤-8"); List<Student> list1 = list.stream().map(Student::new) .collect(Collectors.toList()); } }
|
其他调用方式
使用类名引用成员方法
public class Demo05 { public static void main(String[] args) { List<String> list = new ArrayList<>(); Collections.addAll(list, "a", "b", "c"); List<String> list1 = list.stream() .map(String::toUpperCase) .collect(Collectors.toList()); System.out.println(list1); } }
|
【注】:被引用方法的形参需要跟抽象方法的第二个形参到最后一个形参保持一致

- 第一个参数:表示被引用方法的调用者,决定了可以引用哪些类中的方法
在stream流中,第一个参数一般都表示流里边每个数据,假设流里边的数据是字符串,那么只能引用String这个类中的方法
- 第二个参数到最后一个参数:和被引用方法的形参保持一致,如果没有第二个参数,说明被引用的方法需要是无参的成员方法
引用数组的构造方法
格式:数据类型[]::new
public class Demo06 { public static void main(String[] args) { List<String> list = new ArrayList<>(); Collections.addAll(list, "a", "b", "c"); String[] array = list.stream() .toArray(String[]::new); } }
|