紙上得來終覺淺,絕知此事要躬行
--陸遊 問渠那得清如許,為有源頭活水來 --朱熹
#類別Collections是一個包裝類。它包含有各種有關集合運算的靜態多態方法。這類不能實例化,就像一個工具類別,服務於Java的Collection框架。
java.lang.Object java.util.Collections
#Collections中常用的方法:
##( 1)sort()排序方法
函數## 函數定義:p#ublic static
自然順序將指定清單依升序排序。
參數:要排序的清單。 函數定義:
public static
參數:list-要排序的清單;c-決定清單順序的比較器。 (2)binarySearch()
二分查找方法## 函數定義:public static
使用二分搜尋法搜尋指定列表,以獲得指定物件,在進行此方法呼叫前比較要將列表元素依照升序排序,否則結果不確定,此方法會執行O(n)次連結遍歷和O(log n)次元素比較。
參數: list-要搜尋的鍊錶,key-要搜尋的鍵。
函數定義: public static
參數: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
T obj),使用指定元素取代指定清單中的所有元素,線性時間運作。
參數:list-使用指定元素填入的列表,obj-用來填入指定列表的元素。 (7)
copy()複製方法函數定義:public static
List extends T> src),將所有元素從一個清單複製到另一個清單。執行此操作後,目標清單中每個已複製元素的索引將等同於來源清單中該元素的索引,目標清單的長度至少必須等於來源清單。
參數:dest-目標列表,src-來源列表。
(8)min()最小值法則 函數定義:public static
,此外,collection中的所有元素都必須是可相互比較的。
### 參數:coll-將決定其最小元素的collection。 ########## 函數定義:public static
參數:coll-將決定其最小元素的collection,comp-用來確定最小元素的比較器。
(9)max()最大方法
函數定義:public static
參數:coll-將決定其最大元素的collection。
函數定義:public static
參數:coll-將確定其最大元素的collection,comp-用來確定最大元素的比較器
#(10)rotate()輪替方法
函數定義:#public static void rotate(List> list,int distance),依照指定的距離輪替指定清單中的元素。
參數:list-要輪換的列表,distance-列表輪換的距離,可以使0、負數或大於list.size()的數。
(11)replaceAll#()取代所有函數
函数定义:public static
参数: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还有很多其他的方法,如下表:
方法摘要 | |
---|---|
# static |
<span style="background-color:inherit; color:rgb(51,51,51)">#addAll</span>(Collection
super T> c, T... elements) 則以所有指定元素新增至指定 collection。 |
static |
<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 | 與以先出(Lifo) Queue 的形式返回某個Deque<span style="background-color:inherit; color:rgb(51,51,51)"></span> |
|
#例如」的形式。 <span style="background-color:inherit; color:rgb(51,51,51)"></span> #########static###### |
static |
<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 |
<span style="background-color:inherit; color:rgb(51,51,51)"></span> |
checkedL ist(List |
<span style="background-color:inherit; color:rgb(51,51,51)"></span> static### |
static |
<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 |
<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 |
<span style="background-color:inherit; color:rgb(51,51,51)"></span> |
#emptyList | () 已回空的清單(不變的)。 <span style="background-color:inherit; color:rgb(51,51,51)"></span> | static
#emptyMap | () 已產生空白的對應(不變的)。 <span style="background-color:inherit; color:rgb(51,51,51)"></span> | static
|
# #emptySet() <span style="background-color:inherit; color:rgb(51,51,51)"> 與已回復的set(不變的)。 </span> ############static###### |
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< ;?> c,
Object o)</span> 則傳回指定 collection 中等於指定物件的元素數。 |
static int
|
#indexOfSubList<span style="background-color:inherit; color:rgb(51,51,51)">(List< ;?> source,
List> target)</span> 在指定來源清單中第一次出現指定目標清單的起始位置;如果沒有出現這樣的列表,則傳回 -1。 |
static int
|
#lastIndexOfSubList<span style="background-color:inherit; color:rgb(51,51,51)">(List< ;?> source,
List> target)</span> ## 則傳回指定來源清單中最後一次出現指定目標清單的起始位置;如果沒有出現這樣的列表,則傳回 -1。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)"></span> |
# #list(Enumeration 以 | 列表,它依回傳順序包含指定枚舉傳回的元素。 <span style="background-color:inherit; color:rgb(51,51,51)"></span> static |
#max(Collection extends
T> coll) 依據元素的自然順序 | ,並傳回給定 collection 的最大元素。 <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### |
static |
##min(Collection extends
T> coll, Comparator super T> comp) 依據指定比較器產生的順序,並傳回給定 collection 的最小元素。 |
static
|
<span style="background-color:inherit; color:rgb(51,51,51)"></span>
|
#nCopies(int n, T o) 以指定物件的 | n 個副本所組成的非可變清單。 <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 |
static void |
<span style="background-color:inherit; color:rgb(51,51,51)">reverse</span>(List< ;?> list) 反轉指定清單中元素的順序。 |
static |
<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 |
static void |
<span style="background-color:inherit; color:rgb(51,51,51)">shuffle</span>(List< ;?> list) 使用預設隨機來源對指定清單進行置換。 |
static void |
<span style="background-color:inherit; color:rgb(51,51,51)">shuffle</span>(List< ;?> list,
Random rnd) 使用指定的隨機來源對指定清單進行置換。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)"></span> # #singleton(T o) | 與只包含指定物件的不變set。
static |
<span style="background-color:inherit; color:rgb(51,51,51)"></span> ################################# #singletonList###(T o)### #### 則傳回一個只包含指定物件的不變清單。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">#singletonMap</span>(K key,
V value) 則已傳回一個不可變的映射,且它只將指定鍵對應至指定值。 |
static |
<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< ;?> list,
int i, int j)</span> 則以指定清單的指定位置交換元素。 |
static |
<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 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)"></span> |
| #synchronizedList(List 則已傳回指定清單支援的同步(執行緒安全的)清單。 <span style="background-color:inherit; color:rgb(51,51,51)"></span> #########static###### |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">#synchronizedSortedMap</span>(SortedMap<k> m)</k> 所傳回指定有序且對應的同步(執行緒安全性的)有序對應。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)"></span> |
synchronizedSortedSet | (SortedSet 在已中指定有序set 支援的同步(執行緒安全的)有序set。 <span style="background-color:inherit; color:rgb(51,51,51)"></span> | static
|
<span style="background-color:inherit; color:rgb(51,51,51)"></span> ############################### #unmodifiableCollection###(Collection extends T> c)### ### 以符合指定 collection 的無法修改檢視。 #####################static###### |
static |
<span style="background-color:inherit; color:rgb(51,51,51)">#unmodifiableMap</span>(Map
extends K,? extends V> m) 則已傳回指定對映的無法修改檢視。 |
static |
<span style="background-color:inherit; color:rgb(51,51,51)"></span> # #unmodifiableSet(Set extends T> s) | 以指定 set 的無法修改檢視。
static |
<span style="background-color:inherit; color:rgb(51,51,51)"></span> #unmodifiableSortedMap(SortedMap | 以指定依序對應的無法修改檢視。
static |
<span style="background-color:inherit; color:rgb(51,51,51)"></span> |
以上是Java-collections用法程式碼範例總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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

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

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

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

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


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

Atom編輯器mac版下載
最受歡迎的的開源編輯器

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

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

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