Java中要比较对象的大小或者要对对象的集合进行排序,需要通过比较这些对象的某些属性大小来确定它们之间的大小关系。一般,Java中通过接口实现两个对象的比较,比较常用就是Comparable接口和Comparator接口。首先类要实现接口,并且使用泛型规定要进行比较的对象所属的类,然后类实现了接口后,还需要实现接口定义的比较方法(compareTo方法或者compare方法),在这些方法中传入需要比较大小的另一个对象,通过选定的成员变量与之比较,如果大于则返回1,小于返回-1,相等返回0。
Comparable接口
此接口强行对实现它的每个类的对象进行整体排序。此排序被称为该类的自然排序 ,类的 compareTo方法被称为它的自然比较方法 。实现此接口的对象列表(和数组)可以通过 Collections.sort(和 Arrays.sort )进行自动排序。实现此接口的对象可以用作有序映射表中的键或有序集合中的元素,无需指定比较器。
int compareTo(T o)
比较此对象与指定对象o的顺序。如果该对象小于、等于或大于指定对象o,则分别返回负整数、零或正整数。
参数: o - 指定对象。
返回:负整数、零或正整数,根据此对象判断是小于、等于还是大于指定对象。
抛出:ClassCastException - 如果指定对象的类型不允许它与此对象进行比较。
示例:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class ComparableTest implements Comparable{
private int uid;
private Date datetime;
public ComparableTest(int uid,Date date) {
this.uid = uid;
this.datetime = date;
}
public int compareTo(ComparableTest o) {
if(uido.uid)
return 1;
return datetime.compareTo(o.datetime);
}
@Override
public String toString() {
return "ComparableTest [uid=" + uid + ", datetime=" + datetime + "]";
}
public static void main(String[] args) {
ComparableTest test1 = new ComparableTest(100, new Date());
ComparableTest test2 = new ComparableTest(300, new Date());
ComparableTest test3 = new ComparableTest(200, new Date());
ComparableTest test4 = new ComparableTest(100, new Date());
ComparableTest test5 = new ComparableTest(100, new Date());
List list = new ArrayList();
list.add(test1);
list.add(test2);
list.add(test3);
list.add(test4);
list.add(test5);
System.out.println("排序前:");
for (ComparableTest comparableTest : list) {
System.out.println(comparableTest.toString());
}
Collections.sort(list);//排序
System.out.println("排序后:");
for (ComparableTest comparableTest : list) {
System.out.println(comparableTest.toString());
}
}
}
结果:
排序前:
ComparableTest [uid=100, datetime=Tue Jun 27 14:54:35 CST 2017]
ComparableTest [uid=300, datetime=Tue Jun 27 14:54:35 CST 2017]
ComparableTest [uid=200, datetime=Tue Jun 27 14:54:35 CST 2017]
ComparableTest [uid=100, datetime=Tue Jun 27 14:54:35 CST 2017]
ComparableTest [uid=100, datetime=Tue Jun 27 14:54:35 CST 2017]
排序后:
ComparableTest [uid=100, datetime=Tue Jun 27 14:54:35 CST 2017]
ComparableTest [uid=100, datetime=Tue Jun 27 14:54:35 CST 2017]
ComparableTest [uid=100, datetime=Tue Jun 27 14:54:35 CST 2017]
ComparableTest [uid=200, datetime=Tue Jun 27 14:54:35 CST 2017]
ComparableTest [uid=300, datetime=Tue Jun 27 14:54:35 CST 2017]
Comparator接口
与上面的Comparable接口不同的是:
①、Comparator位于包java.util下,而Comparable位于包java.lang下。
②、Comparable接口将比较代码嵌入需要进行比较的类的自身代码中,而Comparator接口在一个独立的类中实现比较。
③、如果前期类的设计没有考虑到类的Compare问题而没有实现Comparable接口,后期可以通过Comparator接口来实现比较算法进行排序,并且为了使用不同的排序标准做准备,比如:升序、降序。
④、Comparable接口强制进行自然排序,而Comparator接口不强制进行自然排序,可以指定排序顺序。
示例:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
/**
* Comparator的用法示例
*/
public class ComparatorTest implements Comparator {
public int compare(ComparatorClass o1, ComparatorClass o2) {
if(o1.getUid() > o2.getUid())
return 1;
if(o1.getUid() < o2.getUid())
return -1;
return o1.getDatetime().compareTo(o2.getDatetime());
}
public static void main(String[] args) {
ComparatorClass test1 = new ComparatorClass(100, new Date());
ComparatorClass test2 = new ComparatorClass(300, new Date());
ComparatorClass test3 = new ComparatorClass(200, new Date());
ComparatorClass test4 = new ComparatorClass(100, new Date());
ComparatorClass test5 = new ComparatorClass(100, new Date());
List list = new ArrayList();
list.add(test1);
list.add(test2);
list.add(test3);
list.add(test4);
list.add(test5);
System.out.println("ComparatorClass排序前:");
for (ComparatorClass comparatorClass : list) {
System.out.println(comparatorClass.toString());
}
ComparatorTest comparatorTest = new ComparatorTest(); //比较器
Collections.sort(list,comparatorTest);//排序
//list.sort(comparatorTest);
System.out.println("ComparatorClass排序后:");
for (ComparatorClass comparableTest : list) {
System.out.println(comparableTest.toString());
}
}
}
/**
* Comparator需要比较的类
*/
class ComparatorClass{
private int uid;
private Date datetime;
public ComparatorClass(int uid,Date date) {
this.uid = uid;
this.datetime = date;
}
public int getUid() {
return uid;
}
public Date getDatetime() {
return datetime;
}
@Override
public String toString() {
return "ComparatorClass [uid=" + uid + ", datetime=" + datetime + "]";
}
}
结果:
ComparatorClass排序前:
ComparatorClass [uid=100, datetime=Tue Jun 27 15:15:20 CST 2017]
ComparatorClass [uid=300, datetime=Tue Jun 27 15:15:20 CST 2017]
ComparatorClass [uid=200, datetime=Tue Jun 27 15:15:20 CST 2017]
ComparatorClass [uid=100, datetime=Tue Jun 27 15:15:20 CST 2017]
ComparatorClass [uid=100, datetime=Tue Jun 27 15:15:20 CST 2017]
ComparatorClass排序后:
ComparatorClass [uid=100, datetime=Tue Jun 27 15:15:20 CST 2017]
ComparatorClass [uid=100, datetime=Tue Jun 27 15:15:20 CST 2017]
ComparatorClass [uid=100, datetime=Tue Jun 27 15:15:20 CST 2017]
ComparatorClass [uid=200, datetime=Tue Jun 27 15:15:20 CST 2017]
ComparatorClass [uid=300, datetime=Tue Jun 27 15:15:20 CST 2017]