A:让元素本身具备比较性
实现Compareable接口中的compareTo方法。
B:让集合具备比较性
实现Comparator接口中的compare方法。
这是TreeSet的两种排序方式,所以如果定义的集合是TreeSet的 ,要对将要存入的元素排序时,可以用这两种方法;
Comparable是Java.lang包中的借口,可以通过元素实现接口的方式使元素自身具备比较性,必须在子类中复写compareTo方法;
Comparator是Java.Util包中的接口,让集合具备比较性,实现compartor接口,覆盖compare方法,实现一个比较器,在创建集合时将比较器传入
:
TreeSet ts = new TreeSet(new cmp());
应为TreeSet底层是TreeMap实现的,所以想要比较TreeMap也可以使用这两种方法
1. 使用Comparable排序
import java.util.*;
public class ComparableTest{
public static void main(String[] args ){
ArrayList<DVDInfo> dvdList = new ArrayList<DVDInfo>();
populateList(dvdList); // adds the file data to the ArrayList
System.out.println("Before Sorting:");
System.out.println(dvdList);
Collections.sort(dvdList);
System.out.println("After Sorting:");
System.out.println(dvdList);
}
class DVDInfo implements Comparable<DVDInfo> {
String title;
String genre;
String leadActor;
DVDInfo(String t, String g, String a) {
title = t; genre = g; leadActor = a;
}
public String toString() {
return title + " " + genre + " " + leadActor + "/n";
}
public int compareTo(DVDInfo d) {
return title.compareTo(d.getTitle());
}
public String getTitle() {
return title;
}
// other getters and setters
}
public static void populateList(ArrayList<DVDInfo> dvdList){
//Call inner class from a static context. use new OuterClass.new InnerClass()
DVDInfo di1 = new ComparableTest().new DVDInfo("Matrix", "Science Fiction", "Keanu Reeves");
DVDInfo di2 = new ComparableTest().new DVDInfo("Forrest Gump", "Comedy-drama", "Tom Hanks");
DVDInfo di3 = new ComparableTest().new DVDInfo("Mission Impossible", "Action", "Tom Cruise");
dvdList.add(di1);
dvdList.add(di2);
dvdList.add(di3);
}
}
/*
Result:
Before Sorting:
[Matrix Science Fiction Keanu Reeves
, Forrest Gump Comedy-drama Tom Hanks
, Mission Impossible Action Tom Cruise
]
After Sorting:
[Forrest Gump Comedy-drama Tom Hanks
, Matrix Science Fiction Keanu Reeves
, Mission Impossible Action Tom Cruise
]
2. 使用Comparator排序
import java.util.*;
public class ComparatorTest{
public static void main(String[] args ){
ArrayList<DVDInfo> dvdList = new ArrayList<DVDInfo>();
populateList(dvdList); // adds the file data to the ArrayList
System.out.println("Before Sorting:");
System.out.println(dvdList);
TitleComparator tc = new ComparatorTest().new TitleComparator();
Collections.sort(dvdList, tc);
System.out.println("After Sorting:");
System.out.println(dvdList);
}
class TitleComparator implements Comparator<DVDInfo>{
public int compare(DVDInfo one, DVDInfo two){
return one.getTitle().compareTo(two.getTitle());
}
}
//No need to implement Comparable
class DVDInfo {
String title;
String genre;
String leadActor;
DVDInfo(String t, String g, String a) {
title = t; genre = g; leadActor = a;
}
public String toString() {
return title + " " + genre + " " + leadActor + "/n";
}
public String getTitle() {
return title;
}
// other getters and setters
}
public static void populateList(ArrayList<DVDInfo> dvdList){
//Call inner class from a static context. use new OuterClass.new InnerClass()
DVDInfo di1 = new ComparatorTest().new DVDInfo("Matrix", "Science Fiction", "Keanu Reeves");
DVDInfo di2 = new ComparatorTest().new DVDInfo("Forrest Gump", "Comedy-drama", "Tom Hanks");
DVDInfo di3 = new ComparatorTest().new DVDInfo("Mission Impossible", "Action", "Tom Cruise");
dvdList.add(di1);
dvdList.add(di2);
dvdList.add(di3);
}
}
/*
Result:
Before Sorting:
[Matrix Science Fiction Keanu Reeves
, Forrest Gump Comedy-drama Tom Hanks
, Mission Impossible Action Tom Cruise
]
After Sorting:
[Forrest Gump Comedy-drama Tom Hanks
, Matrix Science Fiction Keanu Reeves
, Mission Impossible Action Tom Cruise
]
*/
|