黑马程序员技术交流社区
标题:
Comparable的compareTo方法和Compartor的compare方法区别
[打印本页]
作者:
吴通
时间:
2012-8-28 21:44
标题:
Comparable的compareTo方法和Compartor的compare方法区别
import java.util.*;
class Student implements Comparable<Student>
{
private String name;
private int age;
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age=age;
}
public String getAge()
{
return age;
}
//让这个类有自然比较顺序
public int compareTo(Student s)
{
int num=new Integer(this,age).compareTo(new Integer(s.age));
if(num==0)
return this.name.compareTo(s.name);
return num;
}
public String toString()
{
return name+":::"+age;
}
//复写hashCode和equals方法
public int hashCode()
{
return name.hashCode()+age*34;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s=(Student)obj;
return this.name.equals(s.name)&&this.age==s.age;
}
}
class HashMapTest
{
public static void main(String[] args)
{
HashMap<Student,String> hm=new HashMap<Student,String>();
hm.put(new Student("lisi1",21),"beijing");
hm.put(new Student("lisi2",22),"shanghai");
hm.put(new Student("lisi3",23),"nanjing");
hm.put(new Student("lisi4",24),"wuhan");
//第一种取出方式
Set<Student> keySey=hm.keySet();
Iterator<Student> it=keySet.iterator();
while(it.hasNext())
{
Student stu=it.next();
String addr=hm.get(stu);
System.out.println(stu+"..."+addr);
}
//第二种取出方式 entrySet
Set<Map.Entry<Student,String>> entrySet=hm.entrySet();
Iterator<Map.Entry<Student,String>> it=entrySet.iterator();
while(it.hasNext())
{
Map.Entry<Student,String> me=it.next();
String addr=hm.get(stu);
System.out.println(stu+"..."+addr);
Student stu=me.getKey();
Student addr=me.getValue();
System.out.println(stu+"......."+addr);
}
}
}
上课听老师讲的定义一个对象要让它具有自然的比较性,所以要实现Comparable接口,加入我们不复写父类的比较方式给
这个接口定义特定的比较方式,那么集合也能比较,为什么要多这一步?
还有就是comparable接口和Compartor接口具体怎么应用的?
作者:
方志亮
时间:
2012-8-28 21:54
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
]
*/
作者:
周兴华
时间:
2012-8-28 22:26
TreeSet的排序方式有下面两种:
1:让元素自身具备比较性。元素需要实现Comparable接口,覆盖compareTo方法。
也种方式也成为元素的自然顺序,或者叫做默认顺序。
class Student implements Comparable//该接口强制让学生类具备比较性
{ Student()
{}
public int compareTo(Object obj)
{}
}
2:当元素自身不具备比较性时,或者具备的比较性不是所需要的。这时就需要让集合自身具备比较性。
在集合初始化时,就有了比较方式。
这时就需要定义一个比较器类去实现Comparator,并重写compare方法。
class MyCompare implements Comparator //自定义比较器,使集合具备比较性
{ public int compare(Object o1,Object o2)
{}
}
在定义集合时,需要根据实际情况选择排序方式:
1、TreeSet ts =new TreeSet(); //此时的排序方式为第一种,即元素的自然排序方式。
2、TreeSet ts =new TreeSet(new MyCompare()); //此时的排序方式为第二种,即集合自身具备比较性。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2