4.9 深入理解Arrays.sort(T[],Comparator<? super T>c)

Arrays.sort(T[],Comparator<? super T>c)是一种用于对用户定义的对象数组进行排序的方法。官方的Java Doc 简单地描述它做了什么,但不是深入立即。在这篇文章中,我将介绍关键信息,以便更深入地介绍这种方法。

1.如何用Arrays.sort():一个简单例子

通过阅读下面的例子,你可以对如何使用这个方法有个正确的认识。定义一个比较器,用于按照狗的尺寸来比较大小,然后比较器用排序方法的参数。

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()中使用的策略模式

这是一个很好使用策略模式的例子,值得注意的是为什么策略模式在这里会比较适合。简而言之,策略模式准许在运行时候选择不同的算法。在这种情况下,通过传递不同的比较器,可以选择不同的算法。基于上面的例子,假若你有另外一个比较器,比较狗的重量而不是大小,你可以简单地创建一个新的比较器,如下:

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 

Comparator 仅仅是个接口,任何实现这个接口的比较器都可以在运行时候被使用。这是策略模式的关键思想。

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.总结

1、用通用父类。
2、策略模式。
3、合并排序-nlog(n)时间复杂度。
4、Java.util.Collections.sort(List <T> list,Comparator <?super T> c)与Arrays.sort有类似的思想。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,780评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • Arrays.sort(T[], Comparator < ? super T > c) 是用来对用户自定义的对象...
    汪梓文阅读 187评论 0 0
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,378评论 11 349
  • 一、基本数据类型 注释 单行注释:// 区域注释:/* */ 文档注释:/** */ 数值 对于byte类型而言...
    龙猫小爷阅读 4,292评论 0 16