4.method reference

来源:http://www.cnblogs.com/huiGod/archive/2017/05/21/6885214.html
-Advertisement-
Play Games

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

    }
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1 Mybatis映射文件--增刪改查 POJO類 EmployeeMapper.xml IEmployeeDAO 配置文件--log4j.properties 配置文件--db.properties 配置文件--mybatis-config.xml 測試類 ...
  • 當客戶端訪問某個能開啟會話功能的資源,web伺服器就會創建一個HTTPSession對象,每個HTTPSession對象都會占用一定的記憶體,如果在同一個時間段內訪問的用戶太多,就會消耗大量的伺服器記憶體,為瞭解決這個問題我們使用一種技術:session的持久化。 什麼是session的持久化? 當客戶 ...
  • 1.request.getContextPath()詳解 <%=request.getContextPath()%>是為瞭解決相對路徑的問題,可返回站點的根路徑。 但不用也可以,比如<a href="<%=request.getContextPath()%>/catalog.jsp">,可以直接用< ...
  • 大家是不是都玩過保齡球?雖然水平很爛,但我是保齡球愛好者。今天這一題是用python來計算保齡球的分數。首先講一下保齡球的規則: 保齡球的一局稱為一個frame,一共有10局。 第1到9局,一般每局可以投擲(roll)兩次,但是有一個例外,就是第一次投擲就全中 - 這種情況稱為strike,打出st ...
  • 、 高級語言運行機制 高級語言按程式的執行方式分為編譯型和解釋型兩種。 java語言比較特殊,Java程式的執行必須經過先編譯後解釋的步驟。 1 編譯生成位元組碼,只面向JVM(.class) 2Jvm執行解釋 JVM:(Java virtual machine) java虛擬機負責解釋執行位元組碼文件 ...
  • 流迭代器 2017-05-21 17:05:51 流迭代器是標準模板庫STL中的,是類模板,流迭代器實例化之後即可以和任何接受對應迭代器的函數一起使用(可以將流看做一個容器,把數據存儲在一個連續的緩衝區中,具有迭代器的功能和類似使用)。 istream_iterator 和ostream_itera ...
  • 因為原文中延續了組合模式的代碼示例來講訪問者模式 所以這裡就合併一起來複習了。但主要還是講訪問者模式。顧名思義這個模式會有一個訪問者類(就像近期的熱播劇“人民的名義”中的檢查官,跑到到貪官家裡調查取證,查實後就定罪),被訪問者類調用訪問者類的時候會將自身傳遞給它使用。直接看代碼: //被訪問者基類 ...
  • 1.順序查找 在查找中我們一個一個順序的遍歷表中的所有鍵並使用equals()方法來查找匹配的鍵。 優點:對數組的結構沒有特定的要求,可以使用數組或者鏈表實現,演算法簡單。 缺點:當數組個數n較大時,效率低下。 時間複雜度:查找命中時,最大時間複雜度是O(n),最小時間複雜度是O(1),平均時間複雜度 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...