深入理解 Arrays.sort()
原文地址:http://www.programcreek.com/2013/11/arrays-sort-comparator/
Arrays.sort(T[], Comparator < ? super T > c) 用于对用户定义的对象数组进行排序。官方 Java Doc 简要描述了它的作用,但对于深刻的理解却没有多大的帮助。在这篇文章中,我将介绍一些关键信息,以便更深入地了解它。
1. 如何使用 Arrays.sort()
通过阅读以下示例,你可以快速了解如何正确使用此方法。 Comparator 被定义为比较 Dog size 大小,然后 Comparator 作为参数传入排序方法。
import java.util.Arrays;
import java.util.Comparator;
class Dog{
int size;
public Dog(int s){
size = s;
}
}
class DogSizeComparator implements Comparator<Dog>{
@Override
public int compare(Dog o1, Dog o2) {
return o1.size - o2.size;
}
}
public class ArraySort {
public static void main(String[] args) {
Dog d1 = new Dog(2);
Dog d2 = new Dog(1);
Dog d3 = new Dog(3);
Dog[] dogArray = {d1, d2, d3};
printDogs(dogArray);
Arrays.sort(dogArray, new DogSizeComparator());
printDogs(dogArray);
}
public static void printDogs(Dog[] dogs){
for(Dog d: dogs)
System.out.print(d.size + " " );
System.out.println();
}
}
输出结果为:
2 1 3
1 2 3
2. Arrays.sort() 中的策略模式
上面的代码是一个标准的策略模式。简而言之,策略模式可以在运行时选择不同的算法,在这种情况下,通过传递不同的比较器,选择不同的算法。 基于上面的例子,假设现在你有另一个比较器比较 Dogs,而不是按 size,那么你可以简单地创建一个新的比较器,如下所示:
class Dog{
int size;
int weight;
public Dog(int s, int w){
size = s;
weight = w;
}
}
class DogSizeComparator implements Comparator<Dog>{
@Override
public int compare(Dog o1, Dog o2) {
return o1.size - o2.size;
}
}
class DogWeightComparator implements Comparator<Dog>{
@Override
public int compare(Dog o1, Dog o2) {
return o1.weight - o2.weight;
}
}
public class ArraySort {
public static void main(String[] args) {
Dog d1 = new Dog(2, 50);
Dog d2 = new Dog(1, 30);
Dog d3 = new Dog(3, 40);
Dog[] dogArray = {d1, d2, d3};
printDogs(dogArray);
Arrays.sort(dogArray, new DogSizeComparator());
printDogs(dogArray);
Arrays.sort(dogArray, new DogWeightComparator());
printDogs(dogArray);
}
public static void printDogs(Dog[] dogs){
for(Dog d: dogs)
System.out.print("size="+d.size + " weight=" + d.weight + " ");
System.out.println();
}
}
输出结果:
size=2 weight=50 size=1 weight=30 size=3 weight=40
size=1 weight=30 size=2 weight=50 size=3 weight=40
size=1 weight=30 size=3 weight=40 size=2 weight=50
比较器仅仅是是一个接口,任何实现此接口的比较器都可以在运行时使用,这是策略设计模式的关键思想。
3. 为什么使用 "super"?
很显然,Comparator < T > c 是可以作为参数,但为什么参数也可以是 Comparator< ? super T > c 呢? < ? super T > 表示类型为 T 或者 其超类。为什么它允许超类?答案是:这种方法允许对所有子类使用相同的比较器,我们用个小例子来演示下:
import java.util.Arrays;
import java.util.Comparator;
class Animal{
int size;
}
class Dog extends Animal{
public Dog(int s){
size = s;
}
}
class Cat extends Animal{
public Cat(int s){
size = s;
}
}
class AnimalSizeComparator implements Comparator<Animal>{
@Override
public int compare(Animal o1, Animal o2) {
return o1.size - o2.size;
}
//in this way, all sub classes of Animal can use this comparator.
}
public class ArraySort {
public static void main(String[] args) {
Dog d1 = new Dog(2);
Dog d2 = new Dog(1);
Dog d3 = new Dog(3);
Dog[] dogArray = {d1, d2, d3};
printDogs(dogArray);
Arrays.sort(dogArray, new AnimalSizeComparator());
printDogs(dogArray);
System.out.println();
//when you have an array of Cat, same Comparator can be used.
Cat c1 = new Cat(2);
Cat c2 = new Cat(1);
Cat c3 = new Cat(3);
Cat[] catArray = {c1, c2, c3};
printDogs(catArray);
Arrays.sort(catArray, new AnimalSizeComparator());
printDogs(catArray);
}
public static void printDogs(Animal[] animals){
for(Animal a: animals)
System.out.print("size="+a.size + " ");
System.out.println();
}
}
输出结果为:
size=2 size=1 size=3
size=1 size=2 size=3
size=2 size=1 size=3
size=1 size=2 size=3
4. 总结
用几点总结下 Arrays.sort() :
- 泛型 - 超类
- 策略模式
- 合并排序 - nlog(n) 时间复杂度
Java.util.Collections#sort(List < T > list, Comparator < ? super T > c)与Arrays.sort有着相似的思想
参考阅读:
- http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#sort(T[], java.util.Comparator)