参照方法のJava

この方法は、理由コードより合理化を交換するための式ラムダ式の構文糖、ラムダ式を挙げたように、我々は参照ラムダ式を置き換えることができる方法を指し、参照メソッド関数ポインタとして見ることができると考えることができますラムダ式いくつかのパラメータは、戻り値は、メソッド関数は、4つのカテゴリーに分け引用同じ方法を参照して実装されていること。

1.クラスの静的メソッド名の名前::
@Data
@AllArgsConstructor
public class Student {
    private String name;
    private Integer score;
    public static int compareStudentByScore(Student s1, Student s2) {
        return s1.getScore() - s2.getScore();
    }
    public static int compareStudentByName(Student s1, Student s2) {
        return s1.getName().compareToIgnoreCase(s2.getName());
    }
}
@Test
public void Test() {
        Student s1 = new Student("a", 10);
        Student s2 = new Student("b", 90);
        Student s3 = new Student("c", 50);
        Student s4 = new Student("d", 40);

        List<Student> students = Arrays.asList(s1, s2, s3, s4);

        // 传统lambda方式排序
        students.sort((x, y) -> Student.compareStudentByScore(x, y));
        students.forEach(System.out::println);
        System.out.println("---------------");
        // 利用方法引用排序, 不用传参数在于编译器的自动推断
        students.sort(Student::compareStudentByScore);
        students.forEach(System.out::println);
}
注意:这两个完全不一样,不要搞混了:
classname::staticmethod 方法引用,是个指针
classname.staticmethod 方法的调用
2.参照名(オブジェクト名)::静的メソッド参照に類似している名前を持つインスタンスメソッド、非常に異なっていません
public class StudentComparator {
    public int compareStudentByScore(Student s1, Student s2) {
        return s1.getScore() - s2.getScore();
    }
    public int compareStudentByName(Student s1, Student s2) {
        return s1.getName().compareToIgnoreCase(s2.getName());
    }
}

@Test
public void Test2() {
        Student s1 = new Student("a", 10);
        Student s2 = new Student("b", 90);
        Student s3 = new Student("c", 50);
        Student s4 = new Student("d", 40);

        List<Student> students = Arrays.asList(s1, s2, s3, s4);
        // 传统lambda写法
        StudentComparator comparator = new StudentComparator();
        students.sort((x, y) -> comparator.compareStudentByScore(x, y));
        students.forEach(System.out::println);
        System.out.println("--------------");
        // 对象方法引用写法, 和静态的比较像, 对象和类的区别
        students.sort(comparator::compareStudentByScore);
        students.forEach(System.out::println);
        System.out.println("--------------");
}
3.クラスのインスタンスメソッド名の名前::
@Data
@AllArgsConstructor
public class Student {
    private String name;
    private Integer score;

    public int compareByScore(Student student) {
        return this.getScore() - student.getScore();
    }

    public int compareByName(Student student) {
        return this.getName().compareToIgnoreCase(student.getName());
    }
}

/**
     类::实例方法 形式的方法引用
     这个方法依然是对象调用的, 用的是lambda表达式的第一个参数
     */
    @Test
public void Test3() {
        Student s1 = new Student("a", 10);
        Student s2 = new Student("b", 90);
        Student s3 = new Student("c", 50);
        Student s4 = new Student("d", 40);

        List<Student> students = Arrays.asList(s1, s2, s3, s4);

        students.sort(Student::compareByScore);
        students.forEach(System.out::println);

        System.out.println("-----------------------");
        // 加深理解
        List<String> cities = Arrays.asList("qingdao", "chongqing", "tianjin", "beijing");
        Collections.sort(cities, (c1, c2) -> c1.compareToIgnoreCase(c2));
        Collections.sort(cities, String::compareToIgnoreCase);
}
4.工法参照:クラス名::新しいです
public String getString(Supplier<String> supplier) {
    return supplier.get() + "test";
}

public String getString2(String str, Function<String, String> function) {
    return function.apply(str);
}

/**
* 注意, 这里String::new两次调用的构造方法是不同的, 因为接口的需要不同
* 这个例子是说明 类名::new 这个方法引用会自动选择符合参数约定的方法来调用
*/
@Test
public void Test4() {
    MethodReferenceTest methodReferenceTest = new MethodReferenceTest();
    System.out.println(methodReferenceTest.getString(String::new));
     System.out.println(methodReferenceTest.getString2("hello",String::new));
}

おすすめ

転載: www.cnblogs.com/Lothlorien/p/12023990.html