1.方法引用
1.方法引用概述

注意注意:
1.引用出必须是函数式接口
2.被引用的方法必须已经存在
3.被引用方法的型参和返回值需要跟抽象方法保持一致
4.被引方法的功能要满足当前需求
Arrays.sort(arr,Main::subtraction);
Main是该类的名称,::是方法引用符
     //方法引用
        Integer[] arr={4,3,1,6,2,7,8,5};
        Arrays.sort(arr,run1::subtraction);
        System.out.println(Arrays.toString(arr));
    }
 
    public static int subtraction(int num1,int num2){
        return num2-num1;
    }
 
}
2.引用静态方法
1.方法需要已经存在
2.方法的形参和返回值需要跟抽象方法的形参和返回值保持一致3.方法的功能需要把形参的字符串转换成整数

未利用方法引用

方法引用如下:引用方法parseInt
    ArrayList<String> list10 = new ArrayList<>();
        Collections.addAll(list10, "1","4","3","2","6");
        list10.stream()
                .map(Integer::parseInt)
                .forEach(s->System.out.println(s));3.引用成员方法
格式:对象::成员方法
1.其他类:其他类对象::方法名
2.本类:this::方法名(引用出处不能是父类方法)
3.父类:super::方法名
1.引用其他成员方法

另一个类所定义的方法如/*线以下所示
第五行使用时候需要(对象,方法);这里的对象是在另一个类,所有需要new erer();
public class Main {
    public static void main(String[] args) {
        ArrayList<String> list1=new ArrayList<>();
        Collections.addAll(list1,"张ad","刘df","张ty","周kdh","fjd");
 
        list1.stream().filter(new erer()::stringpd)
                .forEach(s -> System.out.println(s));
    }
 }
 
/*      另一个类的方法如下
 
public class erer {
    public boolean stringpd(String s){
        return s.startsWith("张")&&s.length()==3;
    }
}
 */2.引用本类成员方法
注意:静态方法无this(所以要用本类的对象new Main())
public class Main {
    public static void main(String[] args) {
        ArrayList<String> list1=new ArrayList<>();
        Collections.addAll(list1,"张ad","刘df","张ty","周kdh","fjd");
 
        list1.stream().filter(new Main()::stringpd)
                .forEach(s -> System.out.println(s));
    }
 }
 
 
public class erer {
    public boolean stringpd(String s){
        return s.startsWith("张")&&s.length()==3;
    }
}
3.引用父类成员方法!
拓展:(挺难的)
4.引用构造方法

稿子或者前提:
未引用构造方法:
//匿名内部类    
    List<Student> newlist1 = list1.stream().map(new Function<String, Student>() {
            @Override
            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());
 
        //lambda
        List<Student> new2list1 = list1.stream()
                .map(s -> new Student(s.split(",")[0]
                        , Integer.parseInt(s.split(",")[1])))
                .collect(Collectors.toList());这里要修改Student的录入方式,将(String name,int age)变为(String str)name和age在str中获得。
     //引用构造方法
        List<Student> new3list1 = list1.stream().map(Student::new).collect(Collectors.toList());
        System.out.println(new3list1);
 
 
//其中Strudent这个类里面要加上
  public Student(String str) {
        String[] arr = str.split(",");
        this.name = arr[0];
      this.age = Integer.parseInt(arr[1]);
    }引用构造方法:

5.其他调用方式
1.使用类名引用成员方式
格式:类名::成员方法
范例:String::substring
特有规则:

所以下面才成立:
注意:第一个参数决定了我能引用那个类中的方法。如上图第一个参数是String,引用的类是String。
2.引用数组的构造方式
格式:数据类型[ ]::new
范例:int[ ]::new
 细节:数组的类型,需要跟流中数据的类型保持一致。
细节:数组的类型,需要跟流中数据的类型保持一致。
总结:
6.实战演练
都挺难的!!!
1.转成自定义对象并收集到数组
如下:

Student[] arr的那一行中的第一个Student::new是指引用构造方法,Student[]是指引用数组的构造方法。
2.获取部分属性并收集到数组
如下:

技巧:


















![[240707] X-CMD v0.3.14: cb gh fjo zig 模块增强;新增 lsio 和 pixi 模块](https://i-blog.csdnimg.cn/direct/729696b7ea864b9f8112df29f9c7a880.png#pic_center)

