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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 中的`Comparator`接口 > 原文: [https://beginnersbook.com/2017/08/comparator-interface-in-java/](https://beginnersbook.com/2017/08/comparator-interface-in-java/) 在[最后一個教程](https://beginnersbook.com/2017/08/comparable-interface-in-java-with-example/)中,我們已經了解了如何使用`Comparable`接口對自定義類的對象進行排序。通過使用`Comparable`,我們可以根據任何數據成員對對象進行排序。例如,假設我們有一個`Author`類有數據成員:作者姓名,書名和作者年齡,現在如果我們想根據任何數據成員對對象進行排序那么我們可以使用`Comparable`但是如果我們想要有多個排序選項,并且我們可以根據任何選擇對對象進行排序,這可以使用`Comparator`接口完成,我們可以創建盡可能多的`Comparator`然后我們可以在一個或多個上調用`Collections.sort`這樣的比較器: ```java //Sorting arraylist al by Author Age Collections.sort(al, new AuthorAgeComparator()); //Sorting arraylist al by Book Name Collections.sort(al, new BookNameComparator()); ``` 那么它是怎樣工作的?要像這樣調用`Collections.sort`方法,我們必須首先編寫這些`Comparator `類`AuthorAgeComparator`和`BookNameComparator`,以及`Author`類和`Main`類。 ## 完整的比較示例 `Author.java` ```java public class Author implements Comparable<Author> { String firstName; String bookName; int auAge; Author(String first, String book, int age){ this.firstName = first; this.bookName = book; this.auAge = age; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public int getAuAge() { return auAge; } public void setAuAge(int auAge) { this.auAge = auAge; } @Override /* * When we only use Comparable, this is where we write sorting * logic. This method is called when we implement the Comparable * interface in our class and call Collections.sort() */ public int compareTo(Author au){ ? ? ? ? return this.firstName.compareTo(au.firstName);? ? } } ``` `AuthorAgeComparator.java` ```java import java.util.*; class AuthorAgeComparator implements Comparator<Author>{ public int compare(Author a1,Author a2){ if(a1.auAge==a2.auAge) return 0; else if(a1.auAge>a2.auAge) return 1; else return -1; } } ``` `BookNameComparator.java` ```java import java.util.*;? public class BookNameComparator implements Comparator<Author>{ public int compare(Author a1,Author a2){ ? return a1.bookName.compareTo(a2.bookName); } ? } ``` `SortingPgm.java` ```java import java.util.ArrayList; ? import java.util.Collections; public class SortingPgm{ ?? ? public static void main(String args[]){ ? ? // List of objects of Author class? ? ? ArrayList<Author> al=new ArrayList<Author>(); ?? ? ? al.add(new Author("Henry", "Tropic of Cancer", ?45)); al.add(new Author("Nalo", "Brown Girl in the Ring", 56)); al.add(new Author("Frank", "300", 65)); al.add(new Author("Deborah", "Sky Boys", 51)); al.add(new Author("George R. R.", "A Song of Ice and Fire", 62)); /*? ? ? ? * Sorting the list using Collections.sort() method, we? ? ? ? * can use this method because we have implemented the?? ? ? ? * Comparable interface in our user defined class Author? ? ? ? */? ? ? System.out.println("Sorting by Author First Name:");? ? ? Collections.sort(al); ?? ? ? for(Author au: al){ ?? ? ? System.out.println(au.getFirstName()+", "+au.getBookName()+", "+ au.getAuAge()); ?? ? ? }? /*Sorting using AuthorAgeComparator*/? ? ? System.out.println("Sorting by Author Age:"); Collections.sort(al, new AuthorAgeComparator()); for(Author au: al){ ?? ? ? System.out.println(au.getFirstName()+", "+au.getBookName()+", "+ au.getAuAge()); ?? ? ? }?? ? ??? ? ? /*Sorting using BookNameComparator*/? ? ? System.out.println("Sorting by Book Name:");? ? ? Collections.sort(al, new BookNameComparator());? ? ? for(Author au: al){ ?? ? ? System.out.println(au.getFirstName()+", "+au.getBookName()+", "+? au.getAuAge()); ?? ? ? }?? ? } ? } ? ``` **輸出:** ```java Sorting by Author First Name: Deborah, Sky Boys, 51 Frank, 300, 65 George R. R., A Song of Ice and Fire, 62 Henry, Tropic of Cancer, 45 Nalo, Brown Girl in the Ring, 56 Sorting by Author Age: Henry, Tropic of Cancer, 45 Deborah, Sky Boys, 51 Nalo, Brown Girl in the Ring, 56 George R. R., A Song of Ice and Fire, 62 Frank, 300, 65 Sorting by Book Name: Frank, 300, 65 George R. R., A Song of Ice and Fire, 62 Nalo, Brown Girl in the Ring, 56 Deborah, Sky Boys, 51 Henry, Tropic of Cancer, 45 ```
                  <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>

                              哎呀哎呀视频在线观看