黑马程序员技术交流社区
标题:
Comparable与Comparator的区别
[打印本页]
作者:
宗士为
时间:
2012-5-12 19:54
标题:
Comparable与Comparator的区别
Comparable与Comparator的区别是什么呢,
什么时候用Comparable,什么时候用Comparator
现在只记住了怎么用但是不知道为什么
作者:
王德升
时间:
2012-5-12 20:15
Comparable是一个对象本身就已经支持自比较所需要实现的接口(如String Integer自己就可以完成比较大小操作)。
Comparator是一个专用的比较器,当这个对象不支持自比较或者自比较函数不能满足你的要求时,你可以写一个比较器来完成两个对象之间大小的比较。
换一句话说可以说一个是自已完成比较,一个是外部程序实现比较。
作者:
张成龙
时间:
2012-5-12 20:26
comparable和Comparator都是用来实现集合中的排序的,
1,让元素自身具备比较性。元素需要实现Comparable接口,覆盖compareTo方法;
2,当元素自身不具备比较性时,或者具备的比较性不是所需要的。
这时就需要让集合自身具备比较性,定义比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。
定义一个类,实现Comparator接口,覆盖compare方法。
作者:
黑马-王建雨
时间:
2012-5-12 20:31
这是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.*/
204901qufch9jrq6ewqbhe.jpg
(571.46 KB, 下载次数: 15)
下载附件
2012-5-12 20:30 上传
作者:
张晨
时间:
2012-5-12 20:41
本帖最后由 张晨 于 2012-5-12 20:43 编辑
comparalbe接口用于是已有类具有比较性,必须重写compareTo(T o) 方法。
package TestComparator;
public class Person implements Comparable<Person>{
private int age;
private String name;
public Person(int age, String name) {
super();
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Person p2) { //这里可以使此类本身具有比较性
if(this.getAge() == p2.getAge())
{
return this.getName().compareTo(p2.getName());
}
return this.getAge()-p2.getAge();
}
}
复制代码
package TestComparator;
import java.util.Iterator;
import java.util.TreeSet;
public class TestDemo {
public static void main(String[] argc) {
TreeSet<Person> ts = new TreeSet<Person>(); //注意这里TreeSet()没有传比较器,由于类本身有比较性,所以可以添加元素
ts.add(new Person(22,"p1"));
ts.add(new Person(23,"p2"));
ts.add(new Person(24,"p3"));
for (Iterator<Person> it = ts.iterator(); it.hasNext();)
{
Person person = it.next();
System.out.println(person.getName()+":"+person.getAge());
}
}
}
复制代码
comparator也是一个接口,用于使colletction具有比较性,重写compare方法,通用性比较强。
package TestComparator;
public class Person { //此类本身是没有比较性的
private int age;
private String name;
public Person(int age, String name) {
super();
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
复制代码
package TestComparator;
import java.util.Comparator;
public class Compar implements Comparator<Person> { //独立的比较器,比较两个类的关系
@Override
public int compare(Person p1, Person p2) {
if(p1.getAge() == p2.getAge())
{
return p1.getName().compareTo(p2.getName());
}
return p1.getAge()-p2.getAge();
}
}
复制代码
package TestComparator;
import java.util.Iterator;
import java.util.TreeSet;
public class TestDemo {
public static void main(String[] argc) {
TreeSet<Person> ts = new TreeSet<Person>(new Compar()); //这里必须有比较器,否则会出错
ts.add(new Person(22,"p1"));
ts.add(new Person(23,"p2"));
ts.add(new Person(24,"p3"));
for (Iterator<Person> it = ts.iterator(); it.hasNext();)
{
Person person = it.next();
System.out.println(person.getName()+":"+person.getAge());
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2