搜尋
首頁Javajava教程Java-collections用法程式碼範例總結

紙上得來終覺淺,絕知此事要躬行 --陸遊   問渠那得清如許,為有源頭活水來 --朱熹


#類別Collections是一個包裝類。它包含有各種有關集合運算的靜態態方法。這類不能實例化,就像一個工具類別,服務於Java的Collection框架

java.lang.Object
        java.util.Collections

#Collections中常用的方法:

##( 1)sort()排序方法

        函數##        函數定義:p#ublic static extends Comparable super T>> void sort#(List list)# 根

根據元素的

        自然順序將指定清單依升序排序。

        參數:要排序的清單。         函數定義:

 

public static void sort(List list,Comparator super T> c),根據指定比較器產生的順序對指定清單進行排序。此清單內的所有元素都必須可使用指定比較器相互比較。

        參數:list-要排序的清單;c-決定清單順序的比較器。 (2)binarySearch()

二分查找

方法##        函數定義:public static int binarySearch (List extends Comparable super T>> list,T key

)######

        使用二分搜尋法搜尋指定列表,以獲得指定物件,在進行此方法呼叫前比較要將列表元素依照升序排序,否則結果不確定,此方法會執行O(n)次連結遍歷和O(log n)次元素比較。

        參數: list-要搜尋的鍊錶,key-要搜尋的鍵。

        函數定義: public static int binarySearch(List extends T> list, #T key, Comparator super T> c) 依照指定的比較器將清單升序排序。

        參數:list-要搜尋的列表,key-要搜尋的鍵,c-排序列表的比較器。

(3)reverse()反轉方法

         函#數定義:public static void reverse(List> list),反轉指定清單中元素的順序,此方法以線性時間執行。

        參數:list-元素要被反轉的清單

(4) shuffle()重組方法

       #public static void shuffle(List> list),使用預設隨機來源對指定清單進行置換,所有置換發生的可能性都是大致相等的。

        參數:list-要改組的清單

##        函# #數定義:pub#lic static void shuffle(List> list,Random rnd),使用指定的隨機來源對指定清單進行置換。

    參數:list-要改組的列表,rnd-用來改組列表的隨機來源。

(5)swap()交換方法

#

        函數定義:public static void swap(List> list,#(List> list,int i,

int j),在指定清單的指定位置交換元素。         參數:list-進行元素交換的列表,i-要交換的一個元素的

索引

,j-要交換的另一個元素的索引。

(6)fill()替換方法        函數定義: public static void fill#(List super T> list,

T obj),使用指定元素取代指定清單中的所有元素,線性時間運作。

        參數:list-使用指定元素填入的列表,obj-用來填入指定列表的元素。 (7)

copy

()複製方法函數定義:public static void copy(List super T> dest,

List extends T> src),將所有元素從一個清單複製到另一個清單。執行此操作後,目標清單中每個已複製元素的索引將等同於來源清單中該元素的索引,目標清單的長度至少必須等於來源清單。

        參數:dest-目標列表,src-來源列表。

(8)min()最小值法則        函數定義:public static > T min(Collection extends T> coll),根據元素的自然順序傳回給定Collection的最小元素,Collection中的所有元素必須實作Comparable

介面

,此外,collection中的所有元素都必須是可相互比較的。

###        參數:coll-將決定其最小元素的collection。 ##########

        函數定義:public static min(Collection extends T> coll,Comparator super T> comp),根據指定比較器產生的順序,傳回給定collection的最小元素。

        參數:coll-將決定其最小元素的collection,comp-用來確定最小元素的比較器。

(9)max()最大方法

        函數定義:public static > T max(Collection extends T> coll),根據元素的自然順序,傳回給定collection的最大元素。

        參數:coll-將決定其最大元素的collection。

        函數定義:public static #max(Collection coll,Comparator super T> comp),根據指定比較器產生的順序,傳回給定collection的最大元素。

        參數:coll-將確定其最大元素的collection,comp-用來確定最大元素的比較器

#(10)rotate()輪替方法

        函數定義:#public static void rotate(List> list,int distance),依照指定的距離輪替指定清單中的元素。

        參數:list-要輪換的列表,distance-列表輪換的距離,可以使0、負數或大於list.size()的數。

(11)replaceAll#()取代所有函數

        函数定义:public static boolean replaceAll(List list,T oldVal,newVal),使用另一个值替换列表总出现的所有的某一指定值。

        参数:list-在其中进行替换的列表;oldVal-将被替换的原值;newVal-替换oldVald的新值。


示例代码:

public class Hello {
public static void main(String[] args) {
        System.out.println("sort");
        List list=new ArrayList<Double>();
       double array[] = {112, 111, 23, 456, 231 };
        for (int i = 0; i < array.length; i++) {
            list.add(new Double(array[i]));
        }
        Collections.sort(list);//自然排序
        for (int i = 0; i < array.length; i++) {
            System.out.println(list.get(i));
        }
        System.out.println("shuffle");

        Collections.shuffle(list);//置换
        for (int i = 0; i < array.length; i++) {
            System.out.println(list.get(i));
        }
        Collections.sort(list);//自然排序
        System.out.println("reverse");
        Collections. reverse (list);//反转
        for (int i = 0; i < array.length; i++) {
            System.out.println(list.get(i));
        }
        Collections.sort(list);//自然排序
        System.out.println("copy");
        List li = new ArrayList();
        double arr[] = {1131,333};
        for(int j=0;j<arr.length;j++){
            li.add(new Double(arr[j]));
        }
        Collections.copy(list,li);//拷贝
        for (int i = 0; i <list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println("min");
        System.out.println(Collections.min(list));//返回最小值
        System.out.println("max");
        System.out.println(Collections.max(list));//返回最大值
        System.out.println("rotate");
        Collections.rotate(list,-1);//循环
        for (int i = 0; i <list.size(); i++) {
            System.out.println( list.get(i));
        }
         System.out.println("binarySearch");
        Collections.sort(list);
        System.out.println(list);
        System.out.println(Collections.binarySearch(list, 333.0));//二分查找
    }
}

以上是Collections比较常用的方法,Collections还有很多其他的方法,如下表:


           與以先出(Lifo) Queue 的形式返回某個Deque()static Map()static Set列表,它依回傳順序包含指定枚舉傳回的元素。 ,並傳回給定 collection 的最大元素。 Tn Set# #reverseOrder()#。 Comparator          與只包含指定物件的不變set。 List #synchronizedList(SortedSet s)static Collection          以指定 set 的無法修改檢視。           以指定依序對應的無法修改檢視。
方法摘要
# static boolean <span style="background-color:inherit; color:rgb(51,51,51)">#addAll</span>(Collection super T> c, T... elements) 
          則以所有指定元素新增至指定 collection。
static Queue <span style="background-color:inherit; color:rgb(51,51,51)"><a href="http://www.php.cn/wiki/109.html" target="_blank"></a></span>
#asLifoQueue(Deque deque) <span style="background-color:inherit; color:rgb(51,51,51)"></span>
#例如」的形式。 <span style="background-color:inherit; color:rgb(51,51,51)"></span>
#########static###### int##############binarySearch# ##(List extends Comparable super T>> list, T key)### ###          使用二分搜尋法搜尋指定列表,以取得指定物件。 #####################static###### int##############binarySearch# ##(List extends T> list, T key, Comparator super T> c)### ####          則以二分搜尋法搜尋指定列表,以取得指定物件。
static Collection <span style="background-color:inherit; color:rgb(51,51,51)"></span>
<span style="background-color:inherit; color:rgb(51,51,51)"><a href="http://www.php.cn/wiki/596.html" target="_blank"> #checkedCollection</a>(Collection<e> c, Class<e> type)</e></e></span>           以符合指定 collection 的動態型別
安全
# 檢視。
static List <span style="background-color:inherit; color:rgb(51,51,51)"></span>
checkedList(List list, Class type)           以指定清單的動態型別類型安全檢視。 <span style="background-color:inherit; color:rgb(51,51,51)"></span>static
### ###Map####### ############checkedMap###(Map m, Class keyType, Class valueType)### ###          則傳回指定對映的動態型別安全視圖。 #####################static###### ###Set######################################################################################################### ########checkedSet###(Set s, Class type)### ####          傳回指定 set 的一個動態型別安全視圖。
static SortedMap <span style="background-color:inherit; color:rgb(51,51,51)">#checkedSortedMap</span>(SortedMap<k> m, Class<k> keyType, Class<v> valueType)</v></k></k> 
          則傳回指定有序對應的動態型別安全視圖。
static SortedSet <span style="background-color:inherit; color:rgb(51,51,51)"></span>
checkedSortedSet(SortedSet<e> s, Class<e> type)<span style="background-color:inherit; color:rgb(51,51,51)"> </span>          以 set 指定的動態型別檢視。 </e></e>
static<t> void</t> ##copy(List super T> dest, List extends T> src) <span style="background-color:inherit; color:rgb(51,51,51)">          則將所有元素從一個清單複製到另一個清單。 </span>
############static boolean#####################disjoint###(Collection< ;?> c1, Collection> c2)### ####          若有兩個指定 collection 沒有相同的元素,則傳回 true
static List <span style="background-color:inherit; color:rgb(51,51,51)"></span>
#emptyList           已回空的清單(不變的)。 <span style="background-color:inherit; color:rgb(51,51,51)"></span>
#emptyMap           已產生空白的對應(不變的)。 <span style="background-color:inherit; color:rgb(51,51,51)"></span>
# #emptySet() <span style="background-color:inherit; color:rgb(51,51,51)">          與已回復的set(不變的)。 </span>
############static###### Enumeration############################################### #enumeration###(Collection c)### #####          傳回一個指定 collection 上的列舉。
static<t> void</t> ##fill<span style="background-color:inherit; color:rgb(51,51,51)">(List super T> list, T obj)</span>           以指定元素取代指定清單中的所有元素。
static int frequency<span style="background-color:inherit; color:rgb(51,51,51)">(Collection&lt ;?> c, Object o)</span>           則傳回指定 collection 中等於指定物件的元素數。
static int #indexOfSubList<span style="background-color:inherit; color:rgb(51,51,51)">(List&lt ;?> source, List> target)</span>           在指定來源清單中第一次出現指定目標清單的起始位置;如果沒有出現這樣的列表,則傳回 -1。
static int #lastIndexOfSubList<span style="background-color:inherit; color:rgb(51,51,51)">(List&lt ;?> source, List> target)</span> ##          則傳回指定來源清單中最後一次出現指定目標清單的起始位置;如果沒有出現這樣的列表,則傳回 -1。
static ArrayList <span style="background-color:inherit; color:rgb(51,51,51)"></span>
# #list(Enumeration e)           以陣列<span style="background-color:inherit; color:rgb(51,51,51)"></span>
static> T
#max(Collection extends T> coll)           依據元素的自然順序<span style="background-color:inherit; color:rgb(51,51,51)"></span>
static
##max(Collection extends T> coll, Comparator super T> comp)           依據指定比較器產生的順序,並傳回給定 collection 的最大元素。 <span style="background-color:inherit; color:rgb(51,51,51)"></span>static
###> T######## ########min###(Collection extends T> coll)### ####          依照元素的自然順序 傳回給定 collection 的最小元素。
static T ##min(Collection extends T> coll, Comparator super T> comp)           依據指定比較器產生的順序,並傳回給定 collection 的最小元素。
static List <span style="background-color:inherit; color:rgb(51,51,51)"></span>
#nCopies(int n, T o)           以指定物件的  個副本所組成的非可變清單。 <span style="background-color:inherit; color:rgb(51,51,51)"></span>
static
<span style="background-color:inherit; color:rgb(51,51,51)"></span>
######### #newSetFromMap###(Map map)### ###          則傳回指定對應支援的set。 #####################static###### boolean##############replaceAll# ##(List list, T oldVal, T newVal)### ####          則以另一個值來取代清單中出現的所有某一指定值。
static void <span style="background-color:inherit; color:rgb(51,51,51)">reverse</span>(List&lt ;?> list) 
          反轉指定清單中元素的順序。
static Comparator <span style="background-color:inherit; color:rgb(51,51,51)"></span>
           返回一個比較器,它強行反轉實作了 Comparable介面的物件collection 的自然順序<span style="background-color:inherit; color:rgb(51,51,51)"></span>
static
<span style="background-color:inherit; color:rgb(51,51,51)"></span>
# #reverseOrder###(Comparator cmp)### ###          在一個比較器,而由它強行反轉指定比較器的順序。 #####################static void#####################rotate###(List< ;?> list, int distance)### ####          則以指定的距離輪替指定清單中的元素。
static void <span style="background-color:inherit; color:rgb(51,51,51)">shuffle</span>(List&lt ;?> list) 
          使用預設隨機來源對指定清單進行置換。
static void <span style="background-color:inherit; color:rgb(51,51,51)">shuffle</span>(List&lt ;?> list, Random rnd) 
          使用指定的隨機來源對指定清單進行置換。
static Set <span style="background-color:inherit; color:rgb(51,51,51)"></span># #singleton
(T o)
 
static List<span style="background-color:inherit; color:rgb(51,51,51)"></span>
################################# #singletonList###(T o)### ####          則傳回一個只包含指定物件的不變清單。
static Map <span style="background-color:inherit; color:rgb(51,51,51)">#singletonMap</span>(K key, V value) 
          則已傳回一個不可變的映射,且它只將指定鍵對應至指定值。
static> void <span style="background-color:inherit; color:rgb(51,51,51)">#sort</span>(List<t> list)</t> 
#          則以元素的自然順序 定為指定清單以升序排序。
static<t> void</t> ##sort<span style="background-color:inherit; color:rgb(51,51,51)">(List<t> list, Comparator super T> c)</t></span>           則以指定比較器所產生的順序來排序指定清單。
static void swap<span style="background-color:inherit; color:rgb(51,51,51)">(List&lt ;?> list, int i, int j)</span>           則以指定清單的指定位置交換元素。
static Collection <span style="background-color:inherit; color:rgb(51,51,51)"><a href="http://www.php.cn/wiki/1332.html" target="_blank"></a></span>
# #syn<span style="background-color:inherit; color:rgb(51,51,51)">chr</span>onizedCollection(Collection c)
 
          返回指定collection 支援的同步(線程安全的)。
static <span style="background-color:inherit; color:rgb(51,51,51)"></span>
(List list)           則已傳回指定清單支援的同步(執行緒安全的)清單。 <span style="background-color:inherit; color:rgb(51,51,51)"></span>
#########static###### Map########### #####synchronizedMap###(Map m)### ###          則傳回指定對支援的同步(執行緒安全性的)對映。 #####################static###### Set###################### #synchronizedSet###(Set s)### ####          傳回指定 set 支援的同步(執行緒安全的)set。
static SortedMap <span style="background-color:inherit; color:rgb(51,51,51)">#synchronizedSortedMap</span>(SortedMap<k> m)</k> 
          所傳回指定有序且對應的同步(執行緒安全性的)有序對應。
static SortedSet <span style="background-color:inherit; color:rgb(51,51,51)"></span>
synchronizedSortedSet           在已中指定有序set 支援的同步(執行緒安全的)有序set。 <span style="background-color:inherit; color:rgb(51,51,51)"></span>
<span style="background-color:inherit; color:rgb(51,51,51)"></span>
############################### #unmodifiableCollection###(Collection extends T> c)### ###          以符合指定 collection 的無法修改檢視。 #####################static###### List################################################### #unmodifiableList###(List extends T> list)### ####          傳回指定清單的不可修改檢視。
static Map <span style="background-color:inherit; color:rgb(51,51,51)">#unmodifiableMap</span>(Map extends K,? extends V> m) 
          則已傳回指定對映的無法修改檢視。
static Set <span style="background-color:inherit; color:rgb(51,51,51)"></span># #unmodifiableSet
(Set extends T> s)
 
static SortedMap<span style="background-color:inherit; color:rgb(51,51,51)"></span>#unmodifiableSortedMap
(SortedMap m)
 
static SortedSet<span style="background-color:inherit; color:rgb(51,51,51)"></span>
################# #unmodifiableSortedSet###(SortedSet s)### ###          在已中指定有序set 的不可修改檢視。 ################

以上是Java-collections用法程式碼範例總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何將Maven或Gradle用於高級Java項目管理,構建自動化和依賴性解決方案?如何將Maven或Gradle用於高級Java項目管理,構建自動化和依賴性解決方案?Mar 17, 2025 pm 05:46 PM

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

如何使用適當的版本控制和依賴項管理創建和使用自定義Java庫(JAR文件)?如何使用適當的版本控制和依賴項管理創建和使用自定義Java庫(JAR文件)?Mar 17, 2025 pm 05:45 PM

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

如何使用咖啡因或Guava Cache等庫在Java應用程序中實現多層緩存?如何使用咖啡因或Guava Cache等庫在Java應用程序中實現多層緩存?Mar 17, 2025 pm 05:44 PM

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

如何將JPA(Java持久性API)用於具有高級功能(例如緩存和懶惰加載)的對象相關映射?如何將JPA(Java持久性API)用於具有高級功能(例如緩存和懶惰加載)的對象相關映射?Mar 17, 2025 pm 05:43 PM

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類負載機制如何起作用,包括不同的類載荷及其委託模型?Java的類負載機制如何起作用,包括不同的類載荷及其委託模型?Mar 17, 2025 pm 05:35 PM

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。