There is 4 method of the referenced method: Student class: StudentComparator class: Test:public class MethodReferenceTest { ...
There is 4 method of the referenced method:
Student class:
public class Student { private String name; private int score; public Student(String name, int score) { this.name = name; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public static int compareStudentByScore(Student student1, Student student2) { return student1.getScore() - student2.getScore(); } public static int compareStudentByName(Student student1, Student student2) { return student1.getName().compareToIgnoreCase(student2.getName()); } public int compareByScore(Student student) { return this.getScore() - student.getScore(); } public int compareByName(Student student) { return this.getName().compareToIgnoreCase(student.getName()); } }
StudentComparator class:
public class StudentComparator { public int compareStudentByScore(Student student1, Student student2) { return student1.getScore() - student2.getScore(); } public int compareStudentByName(Student student1, Student student2) { return student1.getName().compareToIgnoreCase(student2.getName()); } }
Test:public class MethodReferenceTest {
public String getString(Supplier<String> supplier) { return supplier.get() + "test"; } public String getString2(String str, Function<String, String> function) { return function.apply(str); } public static void main(String[] args) { Student student1 = new Student("zhangsan", 10); Student student2 = new Student("lisi", 90); Student student3 = new Student("wangwu", 50); Student student4 = new Student("zhaoliu", 40); List<Student> students = Arrays.asList(student1, student2, student3, student4);
/**
* traditional invoke method
*/
// students.sort((studentParam1, studentParam2) -> // Student.compareStudentByScore(studentParam1, studentParam2)); // students.forEach(student -> System.out.println(student.getScore())); // System.out.println("-------");
/**
* Class::staticmethod invoke method
*/
// students.sort(Student::compareStudentByScore); // students.forEach(student -> System.out.println(student.getScore())); // students.sort((studentParam1, studentParam2) -> // Student.compareStudentByName(studentParam1, studentParam2)); // students.forEach(student -> System.out.println(student.getName()));
/**
* Class::instancemethod invoke method
*/
// students.sort(Student::compareStudentByName); // students.forEach(student -> System.out.println(student.getName())); // StudentComparator studentComparator = new StudentComparator(); // students.sort((studentParam1, studentParam2) -> // studentComparator.compareStudentByScore(studentParam1, studentParam2)); // students.forEach(student -> System.out.println(student.getScore()));
/**
* instance::instancemethod invoke method
*/
// students.sort(studentComparator::compareStudentByScore); // students.forEach(student -> System.out.println(student.getScore())); // students.sort((studentParam1, studentParam2) -> // studentComparator.compareStudentByName(studentParam1, studentParam2)); // students.forEach(student -> System.out.println(student.getName())); // students.sort(studentComparator::compareStudentByName); // students.forEach(student -> System.out.println(student.getName())); // students.sort(Student::compareByScore); // students.forEach(student -> System.out.println(student.getScore())); // students.sort(Student::compareByName); // students.forEach(student -> System.out.println(student.getName())); // List<String> cities = Arrays.asList("qingdao", "chongqing", "tianjin", "beijing"); // Collections.sort(cities, (city1, city2) -> city1.compareToIgnoreCase(city2)); // cities.forEach(city -> System.out.println(city));
/**
* Class::new invoke method
*/
// Collections.sort(cities, String::compareToIgnoreCase); // cities.forEach(System.out::println); MethodReferenceTest methodReferenceTest = new MethodReferenceTest(); System.out.println(methodReferenceTest.getString(String::new)); System.out.println(methodReferenceTest.getString2("hello", String::new)); } }