在集合框架中有許多類具有排序功能,如:TreeSet類TreeMap類
爲了用“集合框架”的額外部分把排序支持添加到Java2
SDK,版本1.2,核心Java庫作了許多更改。像String
和Integer
類如今實現Comparable
接口以提供自然排序順序。對於那些沒有自然順序的類、或者當您想要一個不同於自然順序的順序時,您可以實現Comparator
接口來定義您自己的。
Comparable接口
適用於一個類有自然順序的時候(對於這個自然順序的理解,我覺得是這樣:Comparable中只有一個方法compareTo(),它是比較當前實例和作爲參數傳入的元素,一個實例,一個參數,說明實例和參數自然具有可比較性)。
下面是Comparable接口的定義:
package java.lang;
import java.util.*;
public interfaceComparable<T> {
publicintcompareTo(T o);
}
compareTo()方法:如果排序過程中當前實例出現在參數前,就返回某個負值。如果當前實例出現在參數後,則返回正值。否則,返回零。這裏不要求零返回值表示元素相等。零返回值只是表示兩個對象排在同一個位置。
Java1.6中實現了Comparable接口的類大概有50左右個。
Comparator接口
有些類是不能用於實現java.lang.Comparable,這時我們可以提供自己的java.lang.Comparator行爲。或者,如果你不喜歡缺省的Comparable行爲,你也照樣可以提供自己的Comparator。
package java.lang;
import java.util.*;
public interface Comparable<T> {
public int compareTo(T o);
}
實例:
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
/**
* 10、 定義一個學生類, 需要有姓名, 年齡, 考試成績三個成員屬性,
* 創建5個對象, 屬性可爲任意值. 編程對這5個對象按成績排序,並將結果輸出。
* (提示,用TreeSet和Comparator實現)
*
* *******************按成績小>大排序****************
* @author jin
*
*/
public class Test10 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student_ stu1=new Student_("Joyce", 21, 98);
Student_ stu2=new Student_("kelvin", 25, 95);
Student_ stu3=new Student_("kelvun", 29, 89);
Student_ stu4=new Student_("Judy", 25, 92);
Student_ stu5=new Student_("phil", 43, 93);
// 創建TreeSet
Set<Student_> st=new TreeSet<Student_>(new Student_.TestScoresCompare<Student_>());
st.add(stu5); st.add(stu4); st.add(stu3); st.add(stu2); st.add(stu1);
//遍歷
Iterator<Student_> it=st.iterator();
while(it.hasNext()){
Student_ student=it.next();
System.out.println(student.getName()+" : "+student.getAge()+" : "+student.getTest_scores());
}
}
}
class Student_{
// 三個屬性
private String name;
private int age;
private int test_scores;
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public int getTest_scores(){
return this.test_scores;
}
Student_(String name, int age, int test_scores){// 構造器
this.name=name;
this.age=age;
this.test_scores=test_scores;
}
// 這裏爲什麼要用靜態類呢?
static class TestScoresCompare<T> implements Comparator<T>{
// 注意:Comparator<T>是泛型,實現了這個接口的類也要是泛型
//// 實現比較器
@Override
public int compare(T o1, T o2) {
Student_ stu1=(Student_)o1;
Student_ stu2=(Student_)o2;
if(stu1.test_scores>stu2.test_scores){
return 1;
}
return -1;
}
}
// 泛型參數可以出現在:類,接口,成員方法,內嵌類(接口)中
}
另外,“集合框架”提供了兩個特殊的接口:SortedSet接口 和 SortMap接口
SortedSet接口
接口爲集的子集和它的兩端(即頭和尾)提供了訪問方法。
從treeSet類的定義中可以發現,TreeSet中實現了SortedSet接口.
添加到SortedSet中的元素必須實現Comparable接口。或者你必須給他的實現類的構造函數提供一個Comparator(上面那個實例就是這樣呀)。
如果添加元素時比較兩個元素導致了零返回值,那麼新元素就沒有添加進去。
public interface SortedSet<E> extends Set<E> {
Comparator<? super E> comparator(); // 返回與排序有關聯的比較器
SortedSet<E> subSet(E fromElement, E toElement); // 返回指定對象間的元素
SortedSet<E> headSet(E toElement); // 返回從開始到指定元素的集合
SortedSet<E> tailSet(E fromElement);
E first();
E last();
}
SortedMap接口
用來保持鍵Key的有序。
和上面的SortedSet接口類似,爲映射的子集包括兩個端點提供了訪問方法。
TreeMap中實現了SortMap接口。
如果在添加一個鍵-值對時比較兩個鍵產生了零返回值,那麼,原始鍵對應值被新的值替代。