如何在Java中对Collection对象进行排序?
排序集合的對象 (Sorting objects of the Collection)
This concept is related to sorting and here we will see how to sort objects on the Collection?
這個概念與排序有關,在這里我們將看到如何對Collection上的對象進行排序?
In java, we have utility class Collections which provide various methods to perform various task and one of the methods of Collection class is related to sorting like sort().
在Java中,我們有實用程序類Collections,它提供了執(zhí)行各種任務的各種方法,并且Collection類的方法之一與sort()之類的排序有關。
We can implement sorting on Collection object in two ways:
我們可以通過兩種方式對Collection對象實施排序:
- By using Comparable
- By using Comparator
When we call Collections.sort(). It sorts an object based on natural sorting or default sorting(i.e Ascending order) that's is specified in compareTo() method.
當我們調用Collections.sort()時 。 它根據(jù)compareTo()方法中指定的自然排序或默認排序(即升序)對對象進行排序。
When we call Collections.sort(Comparator). It sorts an object based on customized sorting (i.e Ascending order or Descending order) that's is specified in compare() method of Comparator.
當我們調用Collections.sort(Comparator)時 。 它根據(jù)在Comparator的compare()方法中指定的自定義排序(即升序或降序)對對象進行排序。
We will see the sorting ways one by one...
我們將一一看到排序方式...
1)通過使用比較器 (1) By using Comparator)
If we pass the Comparator object in Collection class constructor then our compare() method will be executed.
如果我們在Collection類構造函數(shù)中傳遞Comparator對象,則將執(zhí)行compare()方法。
When we want customize sorting then we should go for Comparator.
當我們想要自定義排序時,我們應該選擇比較器。
It is possible to implement customized sorting by using Comparator interface. (Customized sorting means that according to our need whether it is ascending or descending).
使用Comparator接口可以實現(xiàn)自定義排序。 (自定義排序意味著根據(jù)我們的需要是升序還是降序)。
Example:
例:
import java.util.*;class TreeSetClass {public static void main(String[] args) {// Here we are passing Comparator object in Collection // class constructor for custoize sortingTreeSet ts = new TreeSet(new CustomizeSorting());// adding elements to TreeSetts.add(10);ts.add(40);ts.add(30);ts.add(20);// Customized Sorted ListSystem.out.println("Customize sorting :" + ts);} }// Here we are implementing Comparator interface class CustomizeSorting implements Comparator {// Here we are overrding compare() method of Comparatorpublic int compare(Object obj1, Object obj2) {Integer i1 = (Integer) obj1;Integer i2 = (Integer) obj2;return -i1.compareTo(i2);} }Output
輸出量
E:\Programs>javac TreeSetClass.javaE:\Programs>java TreeSetClass Customize sorting :[40, 30, 20, 10]2)使用可比接口 (2) By using Comparable interface)
For predefined Comparable classes default natural sorting is already available.
對于預定義的可比較類,默認的自然排序已可用。
For predefined Non-Comparable classes default natural sorting is not already available.
對于預定義的“不可比較”類,默認自然排序尚不可用。
For our customized classes to define natural sorting then we should go for Comparable.
為了讓我們的自定義類定義自然排序,我們應該選擇Comparable。
In case of default natural sorting compulsory object should be homogenous and Comparable otherwise we will get CCE (ClassCastException).
在默認情況下,自然排序強制對象應該是同質且可比較的,否則我們將獲得CCE(ClassCastException)。
Example:
例:
import java.util.*;class TreeSetClass {public static void main(String[] args) {Student s1 = new Student(10);Student s2 = new Student(30);Student s3 = new Student(70);Student s4 = new Student(20);// Here we are not passing Comparator object in Collection // class constructor for default sortingTreeSet ts = new TreeSet();// adding elements to TreeSetts.add(s1);ts.add(s2);ts.add(s3);ts.add(s4);// Customized Sorted ListSystem.out.println("Default sorting :" + ts);} }// Here we are implementing Comparable interface class Student implements Comparable {int code;Student(int code) {this.code = code;}public String toString() {return " Code - " + code;}// Here we are overrding compare() method of Comparable interfacepublic int compareTo(Object obj) {int code1 = this.code;Student intermediate = (Student) obj;int code2 = intermediate.code;if (code1 < code2)return -1;else if (code1 > code2)return +1;elsereturn 0;} }Output
輸出量
E:\Programs>javac TreeSetClass.javaE:\Programs>java TreeSetClass Default sorting :[ Code - 10, Code - 20, Code - 30, Code - 70]翻譯自: https://www.includehelp.com/java/how-to-sort-objects-of-the-collection-in-java.aspx
總結
以上是生活随笔為你收集整理的如何在Java中对Collection对象进行排序?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java对数组进行排序_用Java对数组
- 下一篇: 再见Postman,这款API神器更好用