Collections常用方法梳理

Collections (java.util.Collections) 工具類包含了很多有關集合操作的靜態方法,使用這些方法能幫我們簡化代碼。

1. 獲取List中的最小值

 

Java代碼 

 收藏代碼

  1. List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);  
  2. // 6  
  3. System.out.println(java.util.Collections.min(intList));  

 2. 獲取List中的最大值

 

 

Java代碼 

 收藏代碼

  1. List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);  
  2. // 99  
  3. System.out.println(java.util.Collections.max(intList));  

 

 

3. Shuffle

Shuffle方法可以使一個集合的元素亂序化。比如,一副牌的集合爲cardList (類型List<Card>), 使用Collections.shuffle(cardList)就能使一副牌處於亂序,達到洗牌的目的。

 

Java代碼 

 收藏代碼

  1. List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);  
  2. Collections.shuffle(intList);  
  3. // 一次測試的結果  
  4. // [6, 18, 33, 24, 99, 9]  
  5. System.out.println(intList);  

 

4. nCopies

該方法用於返回一個不可變列表組成的n個拷貝的指定對象。

 

Java代碼 

 收藏代碼

  1. // 生成一個由10個100組成的整數列表  
  2. List<Integer> nCopiesList = Collections.nCopies(10, 100);  
  3. //[100, 100, 100, 100, 100, 100, 100, 100, 100, 100]  
  4.     System.out.println(nCopiesList);  

 

5. sort

該方法用於對集合排序。

 

Java代碼 

 收藏代碼

  1. List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);  
  2. Collections.sort(intList);  

 

 

 

上述例子沒有包含Comparator參數,。我們也可以結合Comparator對對象集合進行排序。 比如對存放Person類的對象集按照年齡進行排序。

 

Java代碼 

 收藏代碼

  1. package com.thecodesmaple.example.collection;  
  2.    
  3. public class Person {  
  4.    
  5.     private int age;  
  6.    
  7.     private String firstName;  
  8.    
  9.     private String lastName;  
  10.    
  11.     public Person(int age, String firstName, String lastName) {  
  12.         this.age = age;  
  13.         this.firstName = firstName;  
  14.         this.lastName = lastName;  
  15.     }  
  16.    
  17.     public int getAge() {  
  18.         return age;  
  19.     }  
  20.    
  21.     public void setAge(int age) {  
  22.         this.age = age;  
  23.     }  
  24.    
  25.     public String getFirstName() {  
  26.         return firstName;  
  27.     }  
  28.    
  29.     public void setFirstName(String firstName) {  
  30.         this.firstName = firstName;  
  31.     }  
  32.    
  33.     public String getLastName() {  
  34.         return lastName;  
  35.     }  
  36.    
  37.     public void setLastName(String lastName) {  
  38.         this.lastName = lastName;  
  39.     }  
  40.    
  41.     public String toString() {  
  42.         return "Age: " + age + " FirstName " + firstName + " LastName: "  
  43.                 + lastName;  
  44.     }  
  45.    
  46. }  

 

Java代碼 

 收藏代碼

  1. List<Person> personList = Arrays.asList(new Person(21, "Eric", "W"),  
  2.                 new Person(17, "John", "A"), new Person(28, "Allan", "H"),  
  3.                 new Person(15, "Jonas", "B"));  
  4.         // [Age: 21 FirstName Eric LastName: W, Age: 17 FirstName John LastName:  
  5.         // A, Age: 28 FirstName Allan LastName: H, Age: 15 FirstName Jonas  
  6.         // LastName: B]  
  7.         System.out.println(personList);  
  8.   
  9.         Collections.sort(personList, new Comparator<Person>() {  
  10.   
  11.             @Override  
  12.             public int compare(Person p1, Person p2) {  
  13.                 return p1.getAge() - p2.getAge();  
  14.             }  
  15.   
  16.         });  
  17.   
  18.         // [Age: 15 FirstName Jonas LastName: B, Age: 17 FirstName John  
  19.         // LastName: A, Age: 21 FirstName Eric LastName: W, Age: 28 FirstName  
  20.         // Allan LastName: H]  
  21.         System.out.println("按照年齡排序後:");  
  22.         System.out.println(personList);  

 

 

 

 

6. binarySearch

 

Java代碼  收藏代碼
  1.    List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);  
  2. // 2  
  3. System.out.println(Collections.binarySearch(intList, 18));  
 

 

7. copy

用兩個參數,一個目標 List 和一個源 List, 將源的元素拷貝到目標,並覆蓋它的內容。目標 List至少與源一樣長。

幫助

Java代碼  收藏代碼
  1. List<String> listOne = Arrays.asList("A", "B", "C", "D");  
  2.         List<String> listTwo = Arrays.asList("X", "Y", "Z");  
  3.         Collections.copy(listOne, listTwo);  
  4.         System.out.println(listOne);// [X, Y, Z, D]  
  5.         System.out.println(listTwo);//[X, Y, Z]  
 Java代碼  收藏代碼
  1. List<Person> personList = Arrays.asList(new Person(21, "Eric", "W"),  
  2.                 new Person(17, "John", "A"), new Person(28, "Allan", "H"),  
  3.                 new Person(15, "Jonas", "B"));  
  4.         // [Age: 21 FirstName Eric LastName: W, Age: 17 FirstName John LastName:  
  5.         // A, Age: 28 FirstName Allan LastName: H, Age: 15 FirstName Jonas  
  6.         // LastName: B]  
  7.         System.out.println(personList);  
  8.    
  9.         Collections.sort(personList, new Comparator<Person>() {  
  10.    
  11.             @Override  
  12.             public int compare(Person p1, Person p2) {  
  13.                 return p1.getAge() - p2.getAge();  
  14.             }  
  15.    
  16.         });  
  17.    
  18.         // [Age: 15 FirstName Jonas LastName: B, Age: 17 FirstName John  
  19.         // LastName: A, Age: 21 FirstName Eric LastName: W, Age: 28 FirstName  
  20.         // Allan LastName: H]  
  21.         System.out.println("按照年齡排序後:");  
  22.         System.out.println(personList);  
 

8. disJoint

用於檢查兩個集合有無相同的元素,如果沒有則返回true。

 

 

Java代碼  收藏代碼
  1. List<String> list3 = Arrays.asList("A", "B", "C", "D");  
  2. List<String> list4 = Arrays.asList("X", "Y", "Z");  
  3. boolean disJoint = Collections.disjoint(list3, list4);  
  4. // true  
  5. System.out.println(disJoint);  
 

 

 

9. fill

使用指定元素替換指定列表中的所有元素

 

Java代碼  收藏代碼
  1. List<String> testList = Arrays.asList("A", "B", "C", "D");  
  2. Collections.fill(testList, "Z");  
  3. // [Z, Z, Z, Z]  
  4. System.out.println(testList);  
 

 

 

10. frequency

獲取某個元素在集合中出現的次數。

 

Java代碼  收藏代碼
  1. List<String> testList = Arrays.asList("A", "B", "C", "D");  
  2. int freq = Collections.frequency(testList, "A");  
  3. // 1  
  4. System.out.println(freq);  
 

 

 

11. indexOfSubList

返回指定源列表中第一次出現指定目標列表的起始位置

 

Java代碼  收藏代碼
  1. int index = Collections.indexOfSubList(Arrays.asList("A", "B", "C"),  
  2.                 Arrays.asList("B"));  
  3.         // Print 1  
  4.         System.out.println(index);  
 

 

 

12. lastIndexOfSubList

返回指定源列表中最後一次出現指定目標列表的起始位置

 

Java代碼  收藏代碼
  1. int lastIndex = Collections.lastIndexOfSubList(  
  2.         Arrays.asList("A", "B", "C", "B"), Arrays.asList("B"));  
  3. // Print 3  
  4. System.out.println(lastIndex);  
 

 

 

13. emptyXXX

請參考之前的文章 https://www.cnblogs.com/qiumingcheng/p/7126281.html

 

14. checkedXXX

 

Java代碼 

 收藏代碼

  1. List<String> stringList = Arrays.asList("A", "B", "C", "D");  
  2. List<String> typeSafeList = Collections.checkedList(stringList, String.class);  
  3. //[A, B, C, D]  
  4. System.out.println(typeSafeList);  

 

 

15. reverse

反轉列表中的元素順序。

 

Java代碼 

 收藏代碼

  1. List<String> reverseCandidate = Arrays.asList("A", "B", "C");  
  2.         Collections.reverse(reverseCandidate);  
  3.         // [C, B, A]  
  4.         System.out.println(reverseCandidate);  

 

 

 

16. replaceAll

 

Java代碼 

 收藏代碼

  1. List<String> replaceAllCandidate = Arrays.asList("A", "B", "C");  
  2.         // 將A用Z代替  
  3.         Collections.replaceAll(replaceAllCandidate, "A", "Z");  
  4.         // [Z, B, C]  
  5.         System.out.println(replaceAllCandidate);  

 

 

17. swap

指定一個目標集合以及兩個元素的索引,交換這兩個指定位置元素的值。

 

Java代碼 

 收藏代碼

  1. List<String> swapCandidate = Arrays.asList("A", "B", "C");  
  2.         // 首尾元素交換  
  3.         Collections.swap(swapCandidate, 0, 2);  
  4.         // [C, B, A]  
  5.         System.out.println(swapCandidate);  

 

 

18. synchronizedXXX

 

Java代碼 

 收藏代碼

  1. Collection<String> c = Collections  
  2.                 .synchronizedCollection(new ArrayList<String>());  
  3.         List<String> list = Collections  
  4.                 .synchronizedList(new ArrayList<String>());  
  5.         Set<String> set = Collections.synchronizedSet(new HashSet<String>());  
  6.         Map<String, String> m = Collections  
  7.                 .synchronizedMap(new HashMap<String, String>());  

 

 

19. unmodifiableXXX

 

Java代碼 

 收藏代碼

  1. List<String> unmodifiableList = Collections.unmodifiableList(Arrays  
  2.         .asList("A", "B", "C"));  
  3.  unmodifiableList.add("D");//此動作會拋異常  
  4. // Exception in thread "main" java.lang.UnsupportedOperationException  
  5. // at java.util.Collections$UnmodifiableCollection.add(Unknown Source)  
  6. // at  
  7. // com.thecodesmaple.example.collection.CollectionsExample.main(CollectionsExample.java:149)  

 

 

20. singletonXXX

 

Java代碼 

 收藏代碼

  1. String init[] = { "One", "Two", "Three", "One", "Two", "Three" };  
  2.         List list1 = new ArrayList(Arrays.asList(init));  
  3.         List list2 = new ArrayList(Arrays.asList(init));  
  4.         list1.remove("One");  
  5.         // [Two, Three, One, Two, Three]  
  6.         System.out.println(list1);  
  7.         // [Two, Three, Two, Three]  
  8.         list2.removeAll(Collections.singleton("One"));  
  9.         System.out.println(list2);  

 

 

 

21. rotate

根據指定的距離循環移動指定列表中的元素

 

Java代碼 

 收藏代碼

  1. List<String> rotateList = Arrays.asList("A", "B", "C", "D", "E", "F");  
  2. // [F, A, B, C, D, E]  
  3. // Collections.rotate(rotateList, 1);  
  4. // System.out.println(rotateList);  
  5.    
  6. Collections.rotate(rotateList, 3);  
  7. // [D, E, F, A, B, C]  
  8. System.out.println(rotateList);  

 

 

 

22. reverseOrder

 

Java代碼 

 收藏代碼

  1. List<String> reverseOrderTest = Arrays.asList("A", "B", "C", "D", "E",  
  2.         "F");  
  3. Comparator c = Collections.reverseOrder();  
  4. Collections.sort(reverseOrderTest, c);  
  5. // [F, E, D, C, B, A]  
  6. System.out.println(reverseOrderTest);  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章