<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java 8 帶有 lambda 的`Comparator`示例 > 原文: [https://howtodoinjava.com/java8/comparator-example-lambda/](https://howtodoinjava.com/java8/comparator-example-lambda/) 當我們想要對可以相互比較的對象的[集合](//howtodoinjava.com/java/collections/useful-java-collection-interview-questions/ "Useful java collection interview questions")進行排序時,使用[比較器](https://docs.oracle.com/javase/10/docs/api/java/util/Comparator.html)。 也可以使用`Comparable`接口完成此比較,但是它限制了您只能以一種特定的方式比較這些對象。 如果要基于多個條件/字段對該集合進行排序,則僅需使用`Comparator`。 > **快速參考:** > > ```java > //Compare by Id > Comparator<Employee> compareById_1 = Comparator.comparing(e -> e.getId()); > > Comparator<Employee> compareById_2 = (Employee o1, Employee o2) -> o1.getId().compareTo( o2.getId() ); > > //Compare by firstname > Comparator<Employee> compareByFirstName = Comparator.comparing(e -> e.getFirstName()); > > //how to use comparator > Collections.sort(employees, compareById); > ``` ## 1)概述 為了演示該概念,我將使用具有四個屬性的類`Employee`。 我們將使用它來理解各種用例。 ```java public class Employee { private Integer id; private String firstName; private String lastName; private Integer age; public Employee(Integer id, String firstName, String lastName, Integer age){ this.id = id; this.firstName = firstName; this.lastName = lastName; this.age = age; } //Other getter and setter methods @Override public String toString() { return "\n["+this.id+","+this.firstName+","+this.lastName+","+this.age+"]"; } } ``` 另外,我編寫了一種方法,該方法始終以未排序的順序返回`Employees`的列表。 ```java private static List<Employee> getEmployees(){ List<Employee> employees = new ArrayList<>(); employees.add(new Employee(6,"Yash", "Chopra", 25)); employees.add(new Employee(2,"Aman", "Sharma", 28)); employees.add(new Employee(3,"Aakash", "Yaadav", 52)); employees.add(new Employee(5,"David", "Kameron", 19)); employees.add(new Employee(4,"James", "Hedge", 72)); employees.add(new Employee(8,"Balaji", "Subbu", 88)); employees.add(new Employee(7,"Karan", "Johar", 59)); employees.add(new Employee(1,"Lokesh", "Gupta", 32)); employees.add(new Employee(9,"Vishu", "Bissi", 33)); employees.add(new Employee(10,"Lokesh", "Ramachandran", 60)); return employees; } ``` ## 2)按名字排序 基本用例,其中將根據員工的名字對員工列表進行排序。 ```java List<Employee> employees = getEmployees(); //Sort all employees by first name employees.sort(Comparator.comparing(e -> e.getFirstName())); //OR you can use below employees.sort(Comparator.comparing(Employee::getFirstName)); //Let's print the sorted list System.out.println(employees); Output: //Names are sorted by first name [ [3,Aakash,Yaadav,52], [2,Aman,Sharma,28], [8,Balaji,Subbu,88], [5,David,Kameron,19], [4,James,Hedge,72], [7,Karan,Johar,59], [1,Lokesh,Gupta,32], [10,Lokesh,Ramachandran,60], [9,Vishu,Bissi,33], [6,Yash,Chopra,25] ] ``` ## 3)按名字逆序排序 如果我們想按姓氏排序但又受尊敬的順序怎么辦。 這真的很容易; 使用`reversed()`方法。 ```java List<Employee> employees = getEmployees(); //Sort all employees by first name; And then reversed Comparator<Employee> comparator = Comparator.comparing(e -> e.getFirstName()); employees.sort(comparator.reversed()); //Let's print the sorted list System.out.println(employees); Output: //Names are sorted by first name [[6,Yash,Chopra,25], [9,Vishu,Bissi,33], [1,Lokesh,Gupta,32], [10,Lokesh,Ramachandran,60], [7,Karan,Johar,59], [4,James,Hedge,72], [5,David,Kameron,19], [8,Balaji,Subbu,88], [2,Aman,Sharma,28], [3,Aakash,Yaadav,52]] ``` ## 4)按姓氏排序 我們也可以使用類似的代碼對姓氏進行排序。 ```java List<Employee> employees = getEmployees(); //Sort all employees by first name employees.sort(Comparator.comparing(e -> e.getLastName())); //OR you can use below employees.sort(Comparator.comparing(Employee::getLastName)); //Let's print the sorted list System.out.println(employees); Output: //Names are sorted by first name [[9,Vishu,Bissi,33], [6,Yash,Chopra,25], [1,Lokesh,Gupta,32], [4,James,Hedge,72], [7,Karan,Johar,59], [5,David,Kameron,19], [10,Lokesh,Ramachandran,60], [2,Aman,Sharma,28], [8,Balaji,Subbu,88], [3,Aakash,Yaadav,52]] ``` ## 5)在多個字段上排序 – `thenComparing()` 在這里,我們首先按員工的名字對他們的名單進行排序,然后再對姓氏的列表進行再次排序。 就像我們對 SQL 語句應用排序一樣。 這實際上是一個非常好的特性。 現在,您無需始終對 SQL `select`語句中的多個字段使用排序,也可以在 Java 中對其進行排序。 ```java List<Employee> employees = getEmployees(); //Sorting on multiple fields; Group by. Comparator<Employee> groupByComparator = Comparator.comparing(Employee::getFirstName) .thenComparing(Employee::getLastName); employees.sort(groupByComparator); System.out.println(employees); Output: [3,Aakash,Yaadav,52], [2,Aman,Sharma,28], [8,Balaji,Subbu,88], [5,David,Kameron,19], [4,James,Hedge,72], [7,Karan,Johar,59], [1,Lokesh,Gupta,32], //These both employees are [10,Lokesh,Ramachandran,60], //sorted on last name as well [9,Vishu,Bissi,33], [6,Yash,Chopra,25] ``` ## 5)并行排序(具有多個線程) 您還可以使用多個線程并行地對對象集合進行排序。 如果集合足夠大,可以容納數千個對象,那么它將非常快。 對于少量對象,常規排序就足夠了,建議使用。 ```java //Parallel Sorting Employee[] employeesArray = employees.toArray(new Employee[employees.size()]); //Parallel sorting Arrays.parallelSort(employeesArray, groupByComparator); System.out.println(employeesArray); Output: [3,Aakash,Yaadav,52], [2,Aman,Sharma,28], [8,Balaji,Subbu,88], [5,David,Kameron,19], [4,James,Hedge,72], [7,Karan,Johar,59], [1,Lokesh,Gupta,32], //These both employees are [10,Lokesh,Ramachandran,60], //sorted on last name as well [9,Vishu,Bissi,33], [6,Yash,Chopra,25] ``` 這就是使用帶有`Comparator`的 lambda 對對象進行排序的全部。 如果您了解有關此概念的更多技術,請與我們所有人共享。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看