Java—Sort排序

Java中Sort排序是非常常用的方法,這一章我們主要來認識一下Sort的用法和相關的實現

一、數組Sort排序

升序排序,直接使用Arrays.Sort方法,例如:

int[] array = {10, 3, 6, 1, 4, 5, 9};
//正序排序
Arrays.sort(array);//會檢查數組個數大於286且連續性好就使用歸併排序,若小於47使用插入排序,其餘情況使用雙軸快速排序
System.out.println("升序排序:");
for (int num : array) {
        System.out.println(num);
}

降序排序,對於只輸出數組的情況,可以倒敘循環訪問,例如:

//倒序排序
//(1)由於不提供倒排方法,你可以倒敘輸出
System.out.println("降序輸出:");
for (int i = array.length - 1; i >= 0; i--) {
        System.out.println(array[i]);
}

降序排序,對於需要使用數組 的情況,可以創建一個新的數組,然後倒敘訪問賦值,例如:

//(2)或者創建一個新的數組,倒敘保存到新數組
int[] descArray = new int[array.length];
for (int i = 0; i < array.length; i++) {
        descArray[i] = array[array.length - i - 1];
}
System.out.println("新數組降序輸出:");
for (int num : descArray) {
        System.out.println(num);
}

降序排序,可以先將數組轉爲集合,然後使用Collections.reverse()反轉集合,但是對於非引用類型,不可以使用Arrays.asList(),因爲int[]會被當作一個類型,而不是數組。

所以可以使用Guava的Ints.asList()方法實現,該轉換後的集合,實現了List接口的方法,直接將數組轉入內部的數組變量,需要注意它並沒有實現數組的操作方法,例如調用add會報錯:

轉換和排序例如:

//(3)或者使用Guava來實現
List<Integer> integersList = Ints.asList(array);
Collections.reverse(integersList);//冒泡交換
System.out.println("Guava降序輸出:");
for (int num : integersList) {
    System.out.println(num);
}

轉後的集合類是Guava中的IntArrayAsList,其類UML圖如下:


二、集合Sort排序—包裝類

本小節主要是對jdk類庫中的包裝類排序,例如:Integer、String等,這些類都已經重寫了Compare方法,都有默認排序規則,例如對於Integer類型會比較其包裝的值類型大小,對於String類型會以長度最小字符串爲基準,逐一比較相同位置字符的ASCII碼大小,如果都相同則比較字符串的長度。

以Integer爲例子,升序排序:

//Integer集合,正序排序
List<Integer> list = new ArrayList<Integer>(Arrays.asList(10, 3, 6, 1, 4, 5, 9));
Collections.sort(list);
System.out.println("集合正序排序:");
for (Integer num : list) {
        System.out.println(num);
}

返回:

集合正序排序:
1
3
4
5
6
9
10

降序排序:

//倒敘排序
Comparator<Integer> reverseComparator = Collections.reverseOrder();
Collections.sort(list, reverseComparator);
System.out.println("集合倒敘排序:");
for (Integer num : list) {
    System.out.println(num);
}

返回:

集合倒敘排序:
10
9
6
5
4
3
1

三、集合Sort排序—自定義對象

除了兩節所描述的情況,我們還會遇到對於自定義類排序的情況,例如我們現在有一個學生對象,想要根據年齡對其進行排序,學生類Student如下:

public class Student {

    private String name;

    private Integer age;

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    /**
     * 爲了更好顯示數據,我們重寫toString()方法.
     * @return 顯示變量的字符串
     */
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

(1) 第一種方式,是實現Comparable接口,重寫接口方法。

該CompareTo()方法,如果指定的數與參數相等返回0;如果指定的數小於參數返回 -1;如果指定的數大於參數返回 1。

對於排序來講,你可以認爲當返回1時,指定的數和參數會進行交換,而非1時則不變,指定數可以當作原本的數組中靠前的數,而參數可以當作靠後的數,又因爲只有靠前數大於靠後數時才返回1,所以大的會被放到後面,此時升序排序(方便記憶)。以此類推,倒序情況則相反。

升序排序,比Student類增加了Comparable接口,並實現升序排序:

public class StudentAsc implements Comparable<StudentAsc> {
    private String name;

    private Integer age;

    public StudentAsc(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public int compareTo(StudentAsc o) {
        if(null == this.age) {
            return -1;
        }
        if(null == o.getAge()) {
            return 1;
        }
        return this.age.compareTo(o.getAge());
    }

    @Override
    public String toString() {
        return "StudentAsc{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

}

方法調用:

//正序排序,年齡爲null時爲小
StudentAsc studentWang = new StudentAsc("王小二", 10);
StudentAsc studentZhang = new StudentAsc("張三", 1);
StudentAsc studentGou = new StudentAsc("狗子", 99);
StudentAsc studentZhao = new StudentAsc("趙六", 40);
StudentAsc studentLi = new StudentAsc("李四", null);
List<StudentAsc> studentAscs = new ArrayList<StudentAsc>(Arrays.asList(studentWang, studentZhang, studentGou, studentZhao, studentLi));
Collections.sort(studentAscs);
System.out.println("自定義對象,升序排序:");
for(StudentAsc studentAsc : studentAscs) {
    System.out.println(studentAsc.toString());
}

返回:

自定義對象,升序排序:
Student{name='李四', age=null}
Student{name='張三', age=1}
Student{name='王小二', age=10}
Student{name='趙六', age=40}
Student{name='狗子', age=99}

降序排序,比Student類增加了Comparable接口,並實現倒序排序

public class StudentDesc implements Comparable<StudentDesc> {
    private String name;

    private Integer age;

    public StudentDesc(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }

    public int compareTo(StudentDesc o) {
        if(null == this.age) {
            return 1;
        }
        if(null == o.getAge()) {
            return -1;
        }
        return o.age.compareTo(this.getAge());
    }

    @Override
    public String toString() {
        return "StudentDesc{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

方法調用:

//降敘排序,年齡爲null時爲最大
StudentDesc studentWang = new StudentDesc("王小二", 10);
StudentDesc studentZhang = new StudentDesc("張三", 1);
StudentDesc studentGou = new StudentDesc("狗子", 99);
StudentDesc studentZhao = new StudentDesc("趙六", 40);
StudentDesc studentLi = new StudentDesc("李四", null);
List<StudentDesc> studentAscs = new ArrayList<StudentDesc>(Arrays.asList(studentWang, studentZhang, studentGou, studentZhao, studentLi));
Collections.sort(studentAscs);
System.out.println("自定義對象,降序排序:");
for(StudentDesc studentAsc : studentAscs) {
    System.out.println(studentAsc.toString());
}

返回:

自定義對象,降序排序:
Student{name='狗子', age=99}
Student{name='趙六', age=40}
Student{name='王小二', age=10}
Student{name='張三', age=1}
Student{name='李四', age=null}

(2)第二種方式,上面實現Comparable接口的方法並不十分靈活,比如對於一個類,在不同的地方需要使用不同的排序,此時再這樣做就會顯的十分繁瑣。因此我們可以通過Collections.sort(List<T> list, Comparator<? super T> c)方法來實現,例子中,我們使用Student類,例子如下:

升序排序:

//升序排序
Student studentWang = new Student("王小二", 10);
Student studentZhang = new Student("張三", 1);
Student studentGou = new Student("狗子", 99);
Student studentZhao = new Student("趙六", 40);
Student studentLi = new Student("李四", null);
List<Student> students = new ArrayList<Student>(Arrays.asList(studentWang, studentZhang, studentGou, studentZhao, studentLi));
Collections.sort(students, new Comparator<Student>() {
    public int compare(Student o1, Student o2) {
        if(null == o1.getAge()) {
            return -1;
        }
        if(null == o2.getAge()) {
            return 1;
        }
        return o1.getAge().compareTo(o2.getAge());
    }
});
System.out.println("自定義對象,升序排序:");
for(Student student : students) {
    System.out.println(student.toString());
}

返回:

自定義對象,升序排序:
Student{name='李四', age=null}
Student{name='張三', age=1}
Student{name='王小二', age=10}
Student{name='趙六', age=40}
Student{name='狗子', age=99}

降序排序:

//降序排序
Student studentWang = new Student("王小二", 10);
Student studentZhang = new Student("張三", 1);
Student studentGou = new Student("狗子", 99);
Student studentZhao = new Student("趙六", 40);
Student studentLi = new Student("李四", null);
List<Student> students = new ArrayList<Student>(Arrays.asList(studentWang, studentZhang, studentGou, studentZhao, studentLi));
Collections.sort(students, new Comparator<Student>() {
    public int compare(Student o1, Student o2) {
        if(null == o1.getAge()) {
            return 1;
        }
        if(null == o2.getAge()) {
            return -1;
        }
        return o2.getAge().compareTo(o1.getAge());
    }
});
System.out.println("自定義對象,降序排序:");
for(Student student : students) {
    System.out.println(student.toString());
}

返回:

自定義對象,降序排序:
Student{name='狗子', age=99}
Student{name='趙六', age=40}
Student{name='王小二', age=10}
Student{name='張三', age=1}
Student{name='李四', age=null}


至此對數組、包裝類集合、自定義集合排序做了總結,希望對大家有所幫助,如果有錯誤或問題可以留言在下方,敬請指正。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章