• method reference


    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<String> 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。

  • 相关阅读:
    JAVA如何调用C/C++方法
    java如何去调用C++的方法详解
    在运行时刻更新功能模块
    运用加密技术保护Java源代码/定制ClassLoader
    JAVA 利用JNI加密class文件/自定义ClassLoader 类
    java本地方法如何调用其他程序函数,方法详解2
    java本地方法如何调用其他程序函数,方法详解
    JAVA本地方法详解,什么是JAVA本地方法?
    java随机生成简体中文取指定长度随机简体中文实用方法
    导入安全证书到jdk步骤详细说明-原
  • 原文地址:https://www.cnblogs.com/koushr/p/5950279.html
Copyright © 2020-2023  润新知