java中Comparor與Comparable的問題

來源:互聯網
上載者:User

標籤:

 

1.Comparator中compare()與Comparable中compareTo()方法的區別

  Treeset集合建立對象後,

    A:如果是空構造,即TreeSet<Student> ts = new TreeSet<Student>();  ,那麼ts.add();,時需要在Student上implements Comparable<Student>介面,在下面重寫compareTo()方法,因為此時的add()調用的是compareTo()方法,底層以紅黑二叉樹,判斷之後,排序

    B:如果是帶參構造,即TreeSet<Student> ts = new TreeSet<Student>(Comparator<? super E>comparator); ,ts.add()時,需要自己寫個MyComparator類implements Comparator<Student>介面,(如果調用的較少的話,可以在建立對象的時候,以內部類的形式重寫方法)或者在在MyComparator中重寫compare()方法.

 

  C:未解決的問題:為什麼把

int num = s1.getName().length() - s2.getName().length();
(二叉樹,左中右順序)
s1與s2換個位置之後,就會倒著排序了,二叉樹裡,不是大的放在右邊嗎?
而且換個位置之後根節點還是第一個數嗎?如果是的話,那麼判斷的時候
數是像哪樣呢?

 1 import java.util.Comparator; 2 import java.util.TreeSet; 3  4 /* 5  * 需求:按照姓名的長度排序 6  *  7  * public TreeSet(Comparator<? super E> comparator):構造一個新的空 TreeSet,它根據指定比較子進行排序。 8  */ 9 public class TreeSetDemo {10     public static void main(String[] args) {11         // 建立集合對象12         // TreeSet<Student> ts = new TreeSet<Student>();13         // 比較子排序14         // TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());15         // 內部類的形式16         TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {17             @Override18             public int compare(Student s1, Student s2) {19                 // 按照姓名的長度排序20                 int num = s1.getName().length() - s2.getName().length();21                 // 當姓名的長度相同時,判斷姓名是否相等22                 int num2 = num == 0 ? s1.getName().compareTo(s2.getName())23                         : num;24                 // 當姓名相同時,判斷年齡是否相等25                 int num3 = num2 == 0 ? s1.getAge() - s2.getAge() : num2;26                 return num3;27             }28         });29         // 建立學生對象30         Student s1 = new Student("小fsd名", 12);31         Student s2 = new Student("小sdf白", 22);32         Student s3 = new Student("小sf黑", 33);33         Student s4 = new Student("小菜", 13);34         Student s5 = new Student("小黑", 33);35         Student s6 = new Student("小sdfsdfc", 13);36         Student s7 = new Student("小dsfsd黑黑", 33);37         Student s8 = new Student("小sfsd菜", 122);38         Student s9 = new Student("小黑", 33);39         Student s10 = new Student("小c", 13);40 41         // 添加元素42         ts.add(s1);43         ts.add(s2);44         ts.add(s3);45         ts.add(s4);46         ts.add(s5);47         ts.add(s6);48         ts.add(s7);49         ts.add(s8);50         ts.add(s9);51         ts.add(s10);52 53         // 遍曆元素54         for (Student s : ts) {55             System.out.println(s);56         }57     }58 }
 1 import java.util.Comparator; 2  3 public class MyComparator implements Comparator<Student> { 4  5     @Override 6     public int compare(Student s1, Student s2) { 7         // 按照姓名的長度排序 8         int num = s1.getName().length() - s2.getName().length(); 9         // 當姓名的長度相同時,判斷姓名是否相等10         int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;11         // 當姓名相同時,判斷年齡是否相等12         int num3 = num2 == 0 ? s1.getAge() - s2.getAge() : num2;13         return num3;14     }15 16 }
 1 //或者public class Student implements Comparable 2 public class Student { 3     // 姓名 4     private String name; 5     // 年齡 6     private int age; 7  8     public Student() { 9         super();10     }11 12     public Student(String name, int age) {13         super();14         this.name = name;15         this.age = age;16     }17 18     public String getName() {19         return name;20     }21 22     public void setName(String name) {23         this.name = name;24     }25 26     public int getAge() {27         return age;28     }29 30     public void setAge(int age) {31         this.age = age;32     }33 34     @Override35     public String toString() {36         return "Student [name=" + name + ", age=" + age + "]";37     }38 39     // @Override40     // public int compareTo(Student s) {41     // // 按照姓名的長度排序42     // int num = this.name.length() - s.name.length();43     // // 當姓名的長度相同時,判斷姓名是否相等44     // int num2 = num == 0 ? this.getName().compareTo(s.getName()) : num;45     // // 當姓名相同時,判斷年齡是否相等46     // int num3 = num2 == 0 ? this.getAge() - this.getAge() : num2;47     // return num3;48     // }49 }

 

java中Comparor與Comparable的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.