这是TreeSet的两种排序方式,所以如果定义的集合是TreeSet的 ,要对将要存入的元素排序时,可以用这两种方法;
Comparable是Java.lang包中的借口,可以通过元素实现接口的方式使元素自身具备比较性,必须在子类中复写compareTo方法;
Comparator是Java.Util包中的接口,让集合具备比较性,实现compartor接口,覆盖compare方法,实现一个比较器,在创建集合时将比较器传入:
TreeSet ts = new TreeSet(new cmp());
应为TreeSet底层是TreeMap实现的,所以想要比较TreeMap也可以使用这两种方法
1. 使用Comparable排序
01.import java.util.*;
02.public class ComparableTest{
03. public static void main(String[] args ){
04. ArrayList<DVDInfo> dvdList = new ArrayList<DVDInfo>();
05. populateList(dvdList); // adds the file data to the ArrayList
06. System.out.println("Before Sorting:");
07. System.out.println(dvdList);
08.
09. Collections.sort(dvdList);
10. System.out.println("After Sorting:");
11. System.out.println(dvdList);
12. }
13.
14. class DVDInfo implements Comparable<DVDInfo> {
15. String title;
16. String genre;
17. String leadActor;
18. DVDInfo(String t, String g, String a) {
19. title = t; genre = g; leadActor = a;
20. }
21. public String toString() {
22. return title + " " + genre + " " + leadActor + "/n";
23. }
24. public int compareTo(DVDInfo d) {
25. return title.compareTo(d.getTitle());
26. }
27. public String getTitle() {
28. return title;
29. }
30. // other getters and setters
31. }
32.
33. public static void populateList(ArrayList<DVDInfo> dvdList){
34. //Call inner class from a static context. use new OuterClass.new InnerClass()
35. DVDInfo di1 = new ComparableTest().new DVDInfo("Matrix", "Science Fiction", "Keanu Reeves");
36. DVDInfo di2 = new ComparableTest().new DVDInfo("Forrest Gump", "Comedy-drama", "Tom Hanks");
37. DVDInfo di3 = new ComparableTest().new DVDInfo("Mission Impossible", "Action", "Tom Cruise");
38. dvdList.add(di1);
39. dvdList.add(di2);
40. dvdList.add(di3);
41. }
42.}
43.
44./*
45.Result:
46.Before Sorting:
47.[Matrix Science Fiction Keanu Reeves
48., Forrest Gump Comedy-drama Tom Hanks
49., Mission Impossible Action Tom Cruise
50.]
51.After Sorting:
52.[Forrest Gump Comedy-drama Tom Hanks
53., Matrix Science Fiction Keanu Reeves
54., Mission Impossible Action Tom Cruise
55.]
2. 使用Comparator排序
01.import java.util.*;
02.public class ComparatorTest{
03. public static void main(String[] args ){
04. ArrayList<DVDInfo> dvdList = new ArrayList<DVDInfo>();
05. populateList(dvdList); // adds the file data to the ArrayList
06. System.out.println("Before Sorting:");
07. System.out.println(dvdList);
08.
09. TitleComparator tc = new ComparatorTest().new TitleComparator();
10. Collections.sort(dvdList, tc);
11. System.out.println("After Sorting:");
12. System.out.println(dvdList);
13. }
14.
15. class TitleComparator implements Comparator<DVDInfo>{
16. public int compare(DVDInfo one, DVDInfo two){
17. return one.getTitle().compareTo(two.getTitle());
18. }
19. }
20.
21. //No need to implement Comparable
22. class DVDInfo {
23. String title;
24. String genre;
25. String leadActor;
26. DVDInfo(String t, String g, String a) {
27. title = t; genre = g; leadActor = a;
28. }
29. public String toString() {
30. return title + " " + genre + " " + leadActor + "/n";
31. }
32.
33. public String getTitle() {
34. return title;
35. }
36. // other getters and setters
37. }
38.
39. public static void populateList(ArrayList<DVDInfo> dvdList){
40. //Call inner class from a static context. use new OuterClass.new InnerClass()
41. DVDInfo di1 = new ComparatorTest().new DVDInfo("Matrix", "Science Fiction", "Keanu Reeves");
42. DVDInfo di2 = new ComparatorTest().new DVDInfo("Forrest Gump", "Comedy-drama", "Tom Hanks");
43. DVDInfo di3 = new ComparatorTest().new DVDInfo("Mission Impossible", "Action", "Tom Cruise");
44. dvdList.add(di1);
45. dvdList.add(di2);
46. dvdList.add(di3);
47. }
48.}
49.
50./*
51.Result:
52.Before Sorting:
53.[Matrix Science Fiction Keanu Reeves
54., Forrest Gump Comedy-drama Tom Hanks
55., Mission Impossible Action Tom Cruise
56.]
57.After Sorting:
58.[Forrest Gump Comedy-drama Tom Hanks
59., Matrix Science Fiction Keanu Reeves
60., Mission Impossible Action Tom Cruise
61.]
62.
63.*/
|
|