4.method reference

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

    }
}
时间: 2024-10-05 17:24:02

4.method reference的相关文章

Method Reference在JDK 8中使用的四个场景

学习过JDK 8的都知道,在JDK 8中两个重要的特性就是Lambda Expression和Stream API. Lambda Expression可以使用在任何需要函数式接口的地方.(只包含一个抽象方法的接口,在JDK 8中,接口可以有默认实现) 而为了进一步简化Lambda Expression的使用. JDK 8还设计了一层语法糖,我们称之为Method Reference. 它使用两个冒号表示:: 下面我来介绍这四个场景. 第一种, 当你的Lambda Expression的实现调用

方法引用(method reference)

目录 方法引用(method reference) 1. 含义 2. 分类 3. 总结 方法引用(method reference) 1. 含义 方法引用实际上是 Lambda 表达式的一种语法糖. 我们可以将方法引用看作是一个"函数指针",function pointer 2. 分类 方法引用共分为 4 类: 类名::静态方法名 引用名(对象名)::实例方法名 类名::实例方法名(较难理解) 构造方法引用:类名::new (实际上就是调用一个类的构造方法来生成这个类的对象) 3. 总

Java Stream &amp; Method Reference

目录 Java Stream & Method Reference 1. Stream流 1.1 概述 1.2 流式思想的概述 1.3 获取流 1.4 常用方法 1.5 练习:集合元素处理(传统方式) 1.6 练习:集合元素处理(Stream流方式) 2. 方法引用 2.1 基本介绍 2.2 通过对象名引用[成员方法] 2.3 通过类名称引用[静态方法] 2.4 通过super引用父类的普通成员方法 2.5 通过this引用本类的普通成员方法 2.6 类的构造器(构造方法)引用 2.7 数组的构

Springfox Reference Documentation

1. Introduction The Springfox suite of java libraries are all about automating the generation of machine and human readable specifications for JSON APIs written using the spring family of projects. Springfox works by examining an application, once, a

JDK8的随笔(04)_Lambda表达式扩展之Method References

Method References 方法参照 对于Method Reference了解之前先了解Lambda表达式. 参看: JDK8的随笔(01)_Lambda表达式是个神马东东,初尝禁果 JDK8的随笔(02)_Lambda表达式进一步探讨 JDK8的随笔(03)_Lambda表达式的变量使用范围讨论 Method References 的出现场景 Lambda表达式本身是一个延迟加载采用实现函数式接口的方式来简短代码提高可读性(刚开始可能会降低可读性,熟了就觉得效率很高),减少各种缩进括号

Default Method in Java 8

Default Methods The section Interfaces describes an example that involves manufacturers of computer-controlled cars who publish industry-standard interfaces that describe which methods can be invoked to operate their cars. What if those computer-cont

PatentTips - Object-oriented processor architecture and operating method

BACKGROUND OF THE INVENTION The present invention relates to processors and computer systems. More specifically, the present invention relates to an object-oriented processor architecture and operating method. A conventional central processing unit (

浅谈Java 8中的方法引用(Method References)

本人接触Java 8的时间不长,对Java 8的一些新特性略有所知.Java 8引入了一些新的编程概念,比如经常用到的 lambda表达式.Stream.Optional以及Function等,让人耳目一新.这些功能其实上手并不是很难,根据别人的代码抄过来改一下,并不要知道内部的实现原理,也可以很熟练地用好这些功能.但是当我深究其中一些细节时,会发现有一些知识的盲区.下面我就来谈一下Java 8中的Method References这个概念. 首先我给出官方对于这一概念的详细解释,https:/

【Bugly干货】关于 Android N 那些你不知道的事儿

今年3月,Google 破天荒提前半年发布了 Android N 开发者预览版.当然,作为一个不合格的谷粉并没有第一时间体验安装,因为至今仍然能够回忆起来去年今日此门中(雾)兴冲冲刷了 Android M Preview 的时候发现各种 Crash就连微信也(不出所料得)中招时自己一脸懵逼的心情.当然,为自己的机智而庆幸并没有过多久,很快就有微信好友(当然也是纯纯的谷粉)反馈微信又双叒叕在 Android 新版本下Crash了……好吧这次我们的时间很充裕,因为 5 个 preview 之后才会发