Comparable
1.什麼是Comparable接口
此接口強行對實現它的每個類的對象進行整體排序。此排序被稱為該類的自然排序 ,類的 compareTo 方法被稱為它的自然比較方法 。實現此接口的對象列表(和數組)可以通過 Collections.sort (和 Arrays.sort )進行自動排序。實現此接口的對象可以用作有序映射表中的鍵或有序集合中的元素,無需指定比較器。 強烈推薦(雖然不是必需的)使自然排序與 equals 一致。所謂與equals一致是指對於類 C 的每一個 e1 和 e2 來說,當且僅當 (e1.compareTo((Object)e2) == 0) 與e1.equals((Object)e2) 具有相同的布爾值時,類 C 的自然排序才叫做與 equals 一致 。
2.實現什麼方法
int compareTo(T o) 比較此對象與指定對象的順序。如果該對象小於、等於或大於指定對象,則分別返回負整數、零或正整數。
強烈推薦 (x.compareTo(y)==0) == (x.equals(y)) 這種做法,但不是 嚴格要求這樣做。一般來說,任何實現 Comparable 接口和違背此條件的類都應該清楚地指出這一事實。推薦如此闡述:“注意:此類具有與 equals 不一致的自然排序。”
參數: o - 要比較的對象。 返回:
負整數、零或正整數,根據此對象是小於、等於還是大於指定對象。 拋出:
ClassCastException - 如果指定對象的類型不允許它與此對象進行比較。
在實現Comparable接口中必須重寫compareTo這個方法.代碼如下:
1 package Demo4;
2
3 public class Person implements Comparable<Person>{
4 private String name;
5 private int age;
6 private char sex;
7
8 public Person(String name, int age, char sex) {
9 super();
10 this.name = name;
11 this.age = age;
12 this.sex = sex;
13 }
14
15 /* (非 Javadoc)
16 * @see java.lang.Object#toString()
17 */
18 @Override
19 public String toString() {
20 return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
21 }
22
//當采用不同的排序方式時,重寫不同的comparaTo方法.此時對應不同的數據類型
23 //public int compareTo(Person o) {
24 // return this.sex-o.sex;
25
26 //public int compareTo(Person o) {
27 // return this.name.compareTo(o.name);
28
29 public int compareTo(Person o) {
30 return this.age-o.age;
31 }
33 }
主程序如下:
1 package Demo4;
2
3 import java.util.TreeSet;
4
5 public class Test {
6 public static void main(String[] args) {
7 TreeSet<Person> treeSet = new TreeSet<Person>();
8 Person p1 = new Person("李四",34,'男');
9 Person p2 = new Person("張三",23,'女');
10 Person p3 = new Person("王五",13,'不');
11 Person p4 = new Person("小二",25,'二');
12 treeSet.add(p1);
13 treeSet.add(p2);
14 treeSet.add(p3);
15 treeSet.add(p4);
16
17 for (Person person : treeSet) {
18 System.out.println(person);
19 }
20 }
21 }
顯示結果會根據選擇排序的方式,按照字典順序排列
Comparator
Comparator與Comparable一樣,都是實現某種需求的接口. 區別:Comparable只能讓儲存對象所在的類實現可比較性,當類中具有多種屬性,並且需要根據不同的屬性來排序時,Comparable是不能完成的.Comparable需要重寫comparaTo方法.
Comparator能使類中的不同屬性按照需求進行排序, 需要重寫compare方法.
comparator實例
1 //創建新的類,具有不同的屬性
2 package Demo5;
3
4 public class Person {
5 private String name;
6 private int age;
7 private char sex;
8 public Person(String name, int age, char sex) {
9 super();
10 this.name = name;
11 this.age = age;
12 this.sex = sex;
13 }
14 public Person() {
15 super();
16 }
17 /**
18 * @return name
19 */
20 public String getName() {
21 return name;
22 }
23 /**
24 * @param nane 要設置的 name
25 */
26 public void setName(String name) {
27 this.name = name;
28 }
29 /**
30 * @return age
31 */
32 public int getAge() {
33 return age;
34 }
35 /**
36 * @param age 要設置的 age
37 */
38 public void setAge(int age) {
39 this.age = age;
40 }
41 /**
42 * @return sex
43 */
44 public char getSex() {
45 return sex;
46 }
47 /**
48 * @param sex 要設置的 sex
49 */
50 public void setSex(char sex) {
51 this.sex = sex;
52 }
53 /* (非 Javadoc)
54 * @see java.lang.Object#toString()
55 */
56 @Override
57 public String toString() {
58 return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
59 };
60
61 }
62
63
64 //創建一個比較器
65 package Demo5;
66
67 import java.util.Comparator;
68
69 public class AgeComparator implements Comparator<Person> {
70
71 @Override
72 public int compare(Person o1, Person o2) {
73 //此處將int型轉換成Integer類,可利用comparaTo進行比較,不轉換則用==判斷
74 if ((new Integer(o1.getAge()).compareTo(new Integer(o2.getAge()))==0)) {
75 if(o1.getName().compareTo(o2.getName())==0){
76 if(o1.getSex()==o2.getSex()){
77 return 0 ;
78 }else{
79 return o1.getSex()-o2.getSex();
80 }
81
82 }else{
83 return o1.getName().compareTo(o2.getName());
84 }
85
86 }
87 return o1.getAge() - o2.getAge();
88
89 }
90 }
91
92 //主函數
93 package Demo5;
94
95 import java.util.TreeSet;
96
97 public class Test {
98 public static void main(String[] args) {
99
100 // 1 NameComparator namecomparator = new NameComparator();
101 // TreeSet<Person> set = new TreeSet<Person>(new NameComparator());
102 // 需要利用哪個比較器就選擇哪個,匿名內部類可以簡化代碼
103 TreeSet<Person> set = new TreeSet<Person>(new AgeComparator());
104 Person p1 = new Person("小紅",23,'不');
105 Person p2 = new Person("小明",19,'女');
106 Person p3 = new Person("小花",16,'男');
107 Person p4 = new Person("現場看",14,'女');
108 Person p5 = new Person("小黑",24,'非');
109 set.add(p1);
110 set.add(p2);
111 set.add(p3);
112 set.add(p4);
113 set.add(p5);
114
115 for (Person person : set) {
116 System.out.println(person);
117 }
118 }
119 }