按對象某屬性排序的幾種方法:
第一種,可以實現邊添加邊排序,需要用到TreeSet。
第二種,用數組存放對象們,但是不需單獨取出某屬性排列好再重存,而是在原數組上用比較器重新排一次序。需要用到Arrays.sort(arr,comparator)。
第三種,用集合類中的list的子類存放對象們,然后排序。需要用到Collections.sort(list,comparator)。
一、TreeSet
創建:
| 序號 | 構造函數的說明 |
|---|---|
| 1 | TreeSet () 此構造函數構造空樹集,將在根據其元素的自然順序按升序排序。 |
| 2 | TreeSet (集合 c) 此構造函數生成樹的集合,它包含的元素的集合 c。 |
| 3 | TreeSet (比較器 comp) 此構造函數構造一個空樹集,將根據給定的比較器進行排序。 |
增:
boolean |
add(E e)
將指定的元素添加到這套,如果它已不存在。
|
boolean |
addAll(Collection<? extends E> c)
在加入這一組指定的集合中添加的所有元素。
|
刪:
boolean |
remove(Object o)
從這一組中移除指定的元素,如果它存在。
|
void |
clear()
從這一組中移除所有元素。
|
查:
Comparator<? super E> |
comparator()
返回用於排序在這集,或
空元素,如果這套使用自然排序其元素的比較。
|
boolean |
contains(Object o)
如果此集合包含指定的元素,則返回
true 。
|
boolean |
isEmpty()
如果此集不包含任何元素,則返回
true 。
|
Iterator<E> |
iterator()
返回迭代器中這套以升序排序的元素。
|
|
|
int |
size()
在這套 (其基數) 中返回的元素的數目。
|
|
遍歷:通過迭代器遍歷。
Iterator it=treeset.iterator();
while(it.hasNext()){
//操作當前結點。
}
代碼實現:
TreeSet是一個有序集合,TreeSet中的元素將按照升序排列。
TreeSet存儲對象的時候, 可以排序, 其中Integer有默認排序方法, String有默認排序方法,,而自定義的類對象存儲的時候則沒有順序,需要自定義排序算法。
如果想把自定義類的對象存入TreeSet進行排序,或者對int,String對象想定義自己的排序方法,有以下兩種方法:
排序的第一種方式:
讓元素自身具備比較性。讓元素實現Comparable接口,覆蓋compareTo方法,在方法內定義比較算法, 根據大小關系, 返回正數負數或零。在使用TreeSet存儲對象的時候, add()方法內部就會自動調用compareTo()方法進行比較, 根據比較結果使用二叉樹形式進行存儲。
排序的第二種方式:
自定義比較器。 定義一個類實現Comparator接口,覆蓋compare方法。將該Comparator接口子類對象傳遞給TreeSet集合構造函數。
第一種:類定義時實現Comparable接口,定義自身的比較算法。
此處以Person類為例,把人名和年齡作為對象屬性,存放進treeset中,按照年齡的升/降序保存。
此處以Person類為例,把人名和年齡作為對象屬性,存放進treeset中,按照年齡的升/降序保存。
1 public class TreeSetTest { 2 3 public static void main(String[] args) { 4 TreeSet people=new TreeSet(); 5 people.add(new Person("小明", 20)); 6 people.add(new Person("小張", 30)); 7 people.add(new Person("小劉", 18)); 8 people.add(new Person("小林", 17)); 9 people.add(new Person("小劉", 35)); 10 11 Iterator it=people.iterator(); 12 while(it.hasNext()){ 13 System.out.println(it.next()); 14 } 15 } 16 17 } 18 class Person implements Comparable{ //定義類時,實現比較接口 19 20 String name; 21 int age; 22 23 public Person() { 24 } 25 26 public Person(String name, int age) { 27 this.name = name; 28 this.age = age; 29 } 30 31 public String toString(){ 32 return "姓名:"+name+",年齡:"+age; 33 } 34 /* 35 compareTo(Object o):參數是從根節點開始依次傳進來的結點,直到確定合適的位置用來安插新節點。 36 方法返回三個值,分別對應三種動作:返回1,則繼續遞進,把新結點與下一層的結點進行比較; 37 返回0,則該屬性值不足以決定兩結點位置區別,再定義其他屬性的比較算法來進一步比較兩對象; 38 返回-1,則新結點優先級大於當前層,往上一層比較; 39 */ 40 public int compareTo(Object o) { 41 Person curr=(Person) o; 42 //int result = this.age<curr.age?1:(this.age==curr.age?0:-1);//降序排列:新插入結點與當前比較層結點的屬性比較,小則返回1,繼續往下一層比較 43 int result = this.age>curr.age?1:(this.age==curr.age?0:-1);//升序排列:新插入結點與當前比較層結點的屬性比較,大則返回1,繼續往下一層比較 44 if(result==0){ 45 result=this.name.compareTo(curr.name);//age相同時,則以姓名的字母序來排列。前面說過,int、string類型是有默認排列算法的,所以此處直接用 46 } 47 return result; 48 } 49 }
第二種:定義Comparator接口的實現類(比較器),在類中定義對象的比較算法。在創建Treeset時把比較器對象傳進去。
1 public class TreeSetTest { 2 3 /** 4 * @param args the command line arguments 5 */ 6 public static void main(String[] args) { 7 TreeSet people=new TreeSet(new MyComparator());//把比較器對象作為TreeSet的構造函數參數 8 people.add(new Person("小明", 20)); 9 people.add(new Person("小張", 30)); 10 people.add(new Person("小劉", 18)); 11 people.add(new Person("小林", 17)); 12 people.add(new Person("小劉", 35)); 13 14 Iterator it=people.iterator();//用迭代器遍歷treeset 15 while(it.hasNext()){ 16 System.out.println(it.next()); 17 } 18 } 19 20 } 21 class Person { 22 23 String name; 24 int age; 25 26 public Person() { 27 } 28 29 public Person(String name, int age) { 30 this.name = name; 31 this.age = age; 32 } 33 34 public String toString(){ 35 return "姓名:"+name+",年齡:"+age; 36 } 37 38 } 39 class MyComparator implements Comparator{//實現Comparator接口,自定義比較器,實現compare方法定義比較算法 40 41 /* 42 compare(o1,o2):參數o1是待插入的結點,o2是從樹根節點逐層遍歷下來的結點,用於當前比較。 43 方法返回三個值,分別對應三種動作:返回1,則繼續遞進,把新結點與下一層的結點進行比較; 44 返回0,則該屬性值不足以決定兩結點位置區別,再定義其他屬性的比較算法來進一步比較兩對象; 45 返回-1,則新結點優先級大於當前層,往上一層比較; 46 47 */ 48 public int compare(Object o1, Object o2) { 49 Person p1=(Person)o1; 50 Person p2=(Person)o2; 51 52 int result=p1.age<p2.age?1:(p1.age==p2.age?0:-1);//降序排列 53 //int result=p1.age<p2.age?1:(p1.age==p2.age?0:-1);//升序排列 54 55 if(result==0){ 56 result=p1.name.compareTo(p2.name); 57 } 58 return result; 59 } 60 61 }
二、用數組存放對象,用比較器改變sort()排序方法
數組本身有默認的排序方法,針對int、string等基本類型有默認的sort()方法。而針對類對象的排序,可以給sort()方法傳進一個比較器對象,賦予其排序的算法。
1 public class ArraysTest { 2 3 public static void main(String[] args) { 4 5 Person[] people=new Person[5]; 6 people[0]=(new Person("小明", 20)); 7 people[1]=(new Person("小張", 30)); 8 people[2]=(new Person("小劉", 18)); 9 people[3]=(new Person("小林", 17)); 10 people[4]=(new Person("小劉", 35)); 11 12 Arrays.sort(people,new MyCompare());//傳進來一個比較器對象,使數組按比較器定義的規則來排序 13 for(Person i:people){ 14 System.out.println(i); 15 } 16 17 } 18 19 } 20 class Person { 21 22 23 24 String name; 25 int age; 26 27 28 29 public Person() { 30 } 31 32 33 34 public Person(String name, int age) { 35 this.name = name; 36 this.age = age; 37 } 38 39 public String toString(){ 40 return "姓名:"+name+",年齡:"+age; 41 } 42 43 } 44 45 46 47 class MyCompare implements Comparator<Person>{//定義比較器 48 49 50 51 /* 52 53 重寫比較方法compare(o1,o2): 54 55 方法傳進來兩個對象,用兩個對象的某屬性進行對比,返回一個int。 56 57 int>0,則o1排在o2后面; 58 59 int<0,則o1排在o2前面; 60 61 int=0,則維持原相對位置,即原來存放時o1、o2的前后地址順序。 62 63 */ 64 public int compare(Person o1, Person o2) { 65 int result; 66 if(o1.age>o2.age){ 67 result=1; 68 } 69 else if(o1.age<o2.age){ 70 result=-1; 71 } 72 else{ 73 result=0; 74 } 75 return result; 76 } 77 78 }
第三種:用list的子類:Vector、ArrayList存放對象們,調用Collections.sort(list,comparator)方法進行排序。
1 public class CollectionsTest { 2 3 public static void main(String[] args) { 4 Vector<Person> people=new Vector<>();//用向量保存對象 5 6 //ArrayList<Person> people=new ArrayList<>()://用ArrayList保存對象。 7 people.add(new Person("小明", 20)); 8 people.add(new Person("小張", 30)); 9 people.add(new Person("小劉", 18)); 10 people.add(new Person("小林", 17)); 11 people.add(new Person("小劉", 35)); 12 Collections.sort(people,new MyComparator());//調用方法進行排序 13 Iterator it=people.iterator(); 14 while(it.hasNext()){ 15 System.out.println(it.next()); 16 } 17 } 18 19 } 20 class Person { 21 22 String name; 23 int age; 24 25 public Person() { 26 } 27 28 public Person(String name, int age) { 29 this.name = name; 30 this.age = age; 31 } 32 33 public String toString(){ 34 return "姓名:"+name+",年齡:"+age; 35 } 36 37 } 38 class MyComparator implements Comparator<Person>{//實現Comparator接口,自定義比較器,實現compare方法定義比較算法 39 40 /* 41 compare(o1,o2):方法傳進兩個對象,根據某屬性進行比較,返回一個int值。 42 int>0,o1排在o2后; 43 int<0,o1排在o2前; 44 int=0,維持原來存放時的相對位置。 45 46 */ 47 public int compare(Person p1, Person p2) { 48 int result=p1.age<p2.age?1:(p1.age==p2.age?0:-1);//降序排列 49 //int result=p1.age<p2.age?1:(p1.age==p2.age?0:-1);//升序排列 50 51 if(result==0){ 52 result=p1.name.compareTo(p2.name); 53 } 54 return result; 55 } 56 57 }
