上次描述了Arrays.sort与Collections.sort的区别,这里讲述一下Comparable与Comparator的接口的区别。
1.Comparable是对象内部需要实现的接口,如果一个对象要想用利用Array.sort进行排序,那么这个对象必须实现Comparable的接口,比较的实现是在对象的内部进行的。
2.Comparator接口是在对象外实现的接口,主要是方便了对没有实现Comparable接口的对象的比较和排序,比如:当我们对已经有的一个类进行排序的时候,这个类又没有实现Comparable接口,那么我们可以利用Collections.sort来排序,同时实现一个Comparator接口接口实现对这个类的排序。写了个测试代码,如下,MyInteger是一个持久化对象,由于MyInteger实现了Comparable接口,所以可以用Arrays.sort来排序,当然也可以通过Collections.sort来排序。
一、持久化对象,MyInteger
/**
*
*/
package com.test.compare;
/**
* 需要排序的持久化类
*
* @author rey
*
*/
public class MyInteger implements Comparable {
private int m_nIngeger = 0;
/**
* 构造函数
*/
public MyInteger(int _nInteger) {
super();
this.setIngeger(_nInteger);
}
/**
* @return the m_nIngeger
*/
public int getIngeger() {
return m_nIngeger;
}
/**
* @param mNIngeger
* the m_nIngeger to set
*/
public void setIngeger(int _nInteger) {
m_nIngeger = _nInteger;
}
@Override
public int compareTo(Object _oObject) {
if (_oObject == null || !(_oObject instanceof MyInteger)) {
return -1;
}
MyInteger anotherMyInteger = (MyInteger) _oObject;
int nAnother = anotherMyInteger.getIngeger();
if (this.getIngeger() < nAnother) {
return -1;
} else if (this.getIngeger() == nAnother) {
return 0;
}
return 1;
}
}
二、测试数据类
package com.test.compare;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
List _sortList = new ArrayList();
_sortList.add(new MyInteger(1));
_sortList.add(new MyInteger(10));
_sortList.add(new MyInteger(15));
_sortList.add(new MyInteger(3));
_sortList.add(new MyInteger(78));
_sortList.add(new MyInteger(45));
_sortList.add(new MyInteger(2));
_sortList.add(new MyInteger(56));
_sortList.add(new MyInteger(35));
// 用Collections.sort来排序
sortByCollection(_sortList);
// 打印信息
System.out.println("Collections.sort的排序结果:");
for (int i = 0; i < _sortList.size(); i++) {
MyInteger aMyInteger = (MyInteger) _sortList.get(i);
if (aMyInteger == null) {
continue;
}
System.out.println(aMyInteger.getIngeger());
}
// 重新构造list对象
_sortList = new ArrayList();
_sortList.add(new MyInteger(1));
_sortList.add(new MyInteger(10));
_sortList.add(new MyInteger(15));
_sortList.add(new MyInteger(3));
_sortList.add(new MyInteger(78));
_sortList.add(new MyInteger(45));
_sortList.add(new MyInteger(2));
_sortList.add(new MyInteger(56));
_sortList.add(new MyInteger(35));
// 用Arrays.sort来排序
Object[] sortArrays = _sortList.toArray();
Arrays.sort(sortArrays);
// 打印信息
System.out.println("Arrays.sort的排序结果:");
for (int i = 0; i < sortArrays.length; i++) {
MyInteger aMyInteger = (MyInteger) sortArrays[i];
if (aMyInteger == null) {
continue;
}
System.out.println(aMyInteger.getIngeger());
}
}
/**
* 从Colections.sort来排序
*
* @param _sortList
*/
private static void sortByCollection(List _sortList) {
if (_sortList == null) {
return;
}
Collections.sort(_sortList, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
MyInteger oneInteger = (MyInteger) o1;
MyInteger anotherInteger = (MyInteger) o2;
if (oneInteger.getIngeger() < anotherInteger.getIngeger()) {
return -1;
} else if (oneInteger.getIngeger() == anotherInteger
.getIngeger()) {
return 0;
}
return 1;
}
});
}
}