日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

如何在Java中对Collection对象进行排序?

發布時間:2025/3/11 java 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在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,它提供了執行各種任務的各種方法,并且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()時 。 它根據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)時 。 它根據在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類構造函數中傳遞Comparator對象,則將執行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接口可以實現自定義排序。 (自定義排序意味著根據我們的需要是升序還是降序)。

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对象进行排序?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。