method reference
阅读原文时间:2023年08月16日阅读:3

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

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) {

    MethodReferenceTest methodReferenceTest = new MethodReferenceTest();

    System.out.println(methodReferenceTest.getString(String::new));  
    System.out.println(methodReferenceTest.getString2("hello", String::new));

    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);

// students.sort((studentParam1, studentParam2) ->
// Student.compareStudentByScore(studentParam1, studentParam2));
// students.forEach(student -> System.out.println(student.getScore()));
//
// System.out.println("-------");

// students.sort(Student::compareStudentByScore);
// students.forEach(student -> System.out.println(student.getScore()));
//
// System.out.println("-------");
//
// students.sort((studentParam1, studentParam2) ->
// Student.compareStudentByName(studentParam1, studentParam2));
// students.forEach(student -> System.out.println(student.getName()));
//
// System.out.println("-------");
//
// students.sort(Student::compareStudentByName);
// students.forEach(student -> System.out.println(student.getName()));
//
// System.out.println("-------");
//
// StudentComparator studentComparator = new StudentComparator();
//
// students.sort((studentParam1, studentParam2) ->
// studentComparator.compareStudentByScore(studentParam1, studentParam2));
// students.forEach(student -> System.out.println(student.getScore()));

// System.out.println("-------");
//
// students.sort(studentComparator::compareStudentByScore);
// students.forEach(student -> System.out.println(student.getScore()));
//
// System.out.println("-------");
//
// students.sort((studentParam1, studentParam2) ->
// studentComparator.compareStudentByName(studentParam1, studentParam2));
// students.forEach(student -> System.out.println(student.getName()));
//
// System.out.println("-------");
//
// students.sort(studentComparator::compareStudentByName);
// students.forEach(student -> System.out.println(student.getName()));
//
// System.out.println("-------");
//
// students.sort(Student::compareByScore);
// students.forEach(student -> System.out.println(student.getScore()));
//
// System.out.println("-------");
//
// students.sort(Student::compareByName);
// students.forEach(student -> System.out.println(student.getName()));
//
// System.out.println("-------");
//
List cities = Arrays.asList("qingdao", "chongqing", "tianjin", "shenzhen");

// Collections.sort(cities, (city1, city2) -> city1.compareToIgnoreCase(city2));
// cities.forEach(city -> System.out.println(city));

// System.out.println("-------");
//
// Collections.sort(cities, String::compareToIgnoreCase);
// cities.forEach(System.out::println);
//
// System.out.println("-------");
//

}  

}

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());  
}  

}

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());  
}  

}

Lambda can be replaced with method reference。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章