collections.sort,Collectionssort方法
Java Collections.sort
package?com.zk.test;
import?java.util.Date;
/**
?*?实现Comparator接口,用于比较
?*
?*/
public?class?Dog?implements?Comparable{
private?String?name;
private?int?age;
private?Date?birthday;
private?String?kinds;
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?int?getAge()?{
return?age;
}
public?void?setAge(int?age)?{
this.age?=?age;
}
public?Date?getBirthday()?{
return?birthday;
}
public?void?setBirthday(Date?birthday)?{
this.birthday?=?birthday;
}
public?String?getKinds()?{
return?kinds;
}
public?void?setKinds(String?kinds)?{
this.kinds?=?kinds;
}
@Override
public?String?toString()?{
return?"Dog?[name="?+?name?+?",?age="?+?age?+?",?birthday="?+?birthday?+?",?kinds="?+?kinds?+?"]";
}
@Override
public?int?compareTo(Object?o)?{
//生日比较
return?this.getBirthday().before(((Dog)o).getBirthday())???-1?:?1;
}
}
package?com.zk.test;
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.Date;
import?java.util.List;
public?class?TestSort?{
public?static?void?main(String[]?args)?{
ListDog?dogs?=?new?ArrayListDog();
for?(int?i?=?0;?i??5;?i++)?{
Dog?dog?=?new?Dog();
dog.setName("狗狗"?+?i);
dog.setAge(i);
dog.setKinds("贵宾犬");
//随机时间?出生日期
dog.setBirthday(new?Date((long)?(System.currentTimeMillis()?-?(Math.random()?*?10000000))));
dogs.add(dog);
}
//排序前
System.out.println("排序前:");
for?(Dog?dog?:?dogs)?{
System.out.println(dog.toString());
}
Collections.sort(dogs);
//排序后
System.out.println("排序后:");
for?(Dog?dog?:?dogs)?{
System.out.println(dog.toString());
}
}
}
Collections.sort和Arrays.sort分析比较
由jdk源码我们可知,Collections类中的sort函数提供了两种重载。
第一种方法:
T extends Comparable? super T该方法的泛型必须实现了Comparable中的compareTo()方法,输入为相应的List。
第二种方法:
对泛型不作要求,但是输入参数中需提供Comparator
我们可以通过例子来分析:
由jdk源码可知,Arrays.sort方法也可以通过自定义Comparator的方法来实现自定义排序。
总结
由上可知,Arrays.sort和Collections.sort实现自定义排序时基本相同,分为两种:
1)被排序的元素自身实现了Comparable接口
2)被排序的元素没有实现Comparable接口,自定义比较器Comparator类作为输入参数输入
区别:
Collection.sort是给ListT进行排序,而Arrays.sort是给数组进行排序。
PS:
回到Collections.sort的源码:
我们发现调用了list.sort方法,查看该方法的源码
调用了Arrays.sort方法。
所以,其实Collections.sort实质上是调用的Arrays.sort
关于Collections.sort(list)方法
可以看看源码,Collections的sort方法如下:
public?static?T?extends?Comparable??super?T?void?sort(ListT?list)?{
Object[]?a?=?list.toArray();
Arrays.sort(a);
ListIteratorT?i?=?list.listIterator();
for?(int?j=0;?ja.length;?j++)?{
????i.next();
????i.set((T)a[j]);
}
????}
可以看到这里实际调用了Arrsys类的sort方法;我们再看看这个方法:
public?static?void?sort(Object[]?a)?{
????????Object[]?aux?=?(Object[])a.clone();
????????mergeSort(aux,?a,?0,?a.length,?0);
????}
这个很简单,调用了另外一个方法mergeSort:
private?static?void?mergeSort(Object[]?src,
??Object[]?dest,
??int?low,
??int?high,
??int?off)?{
int?length?=?high?-?low;
//?Insertion?sort?on?smallest?arrays
????????if?(length??INSERTIONSORT_THRESHOLD)?{
????????????for?(int?i=low;?ihigh;?i++)
????????????????for?(int?j=i;?jlow?
?((Comparable)?dest[j-1]).compareTo(dest[j])0;?j--)
????????????????????swap(dest,?j,?j-1);
????????????return;
????????}
????????//?Recursively?sort?halves?of?dest?into?src
????????int?destLow??=?low;
????????int?destHigh?=?high;
????????low??+=?off;
????????high?+=?off;
????????int?mid?=?(low?+?high)??1;
????????mergeSort(dest,?src,?low,?mid,?-off);
????????mergeSort(dest,?src,?mid,?high,?-off);
????????//?If?list?is?already?sorted,?just?copy?from?src?to?dest.??This?is?an
????????//?optimization?that?results?in?faster?sorts?for?nearly?ordered?lists.
????????if?(((Comparable)src[mid-1]).compareTo(src[mid])?=?0)?{
????????????System.arraycopy(src,?low,?dest,?destLow,?length);
????????????return;
????????}
????????//?Merge?sorted?halves?(now?in?src)?into?dest
????????for(int?i?=?destLow,?p?=?low,?q?=?mid;?i??destHigh;?i++)?{
????????????if?(q?=?high?||?p??mid??((Comparable)src[p]).compareTo(src[q])=0)
????????????????dest[i]?=?src[p++];
????????????else
????????????????dest[i]?=?src[q++];
????????}
????}
看到没!!中间把Object对象强转成了Comparable,并直接调用了compareTo方法!!明白了吧!!