The Java.util.Collections tool class provides a lot of practical methods. Makes it easier for programmers to manipulate collection classes, and these methods are static.
The entire Collections tool class source code is almost identical to 4000 lines. We illustrate some of the typical approaches.
1. Call an empty List,set,map
publicstaticfinalnew EmptyList<>();publicstaticfinalnew EmptyMap<>();publicstaticfinalnew EmptySet<>();
2. AddAll
public static <T> Boolean addall (collection<?
Super t> C, T ... elements): Adds specific elements to the specified set of C elements
Case 2-1:
List<String> List = NewArrayList<>();List.Add"S2");List.Add"S4");List.Add"S1");List.Add"S3"); System.Out.printlnList); Collections.AddAll (List,"S5","S7",NULL,"S9"); System.Out.printlnList);
Execution Result:
null, s9]
3. BinarySearch
public static <T> int BinarySearch (LIST<?
Extends Comparable<?
Super t>> list, T key)
public static <T> int BinarySearch (LIST<?
Extends t> list, T key, comparator<? Super t> C)
Using dichotomy to find elements in a specified set, as for comparable and comparator believe that the friends who have seen some of the Liebovin ahead should be very clear. Detailed also can participate in "comparable and comparator analysis"
4. Sort
public static <t extends Comparable<? Super t>> void Sort (list<t> List) requires the generic T class itself to support the comparable interface
public static <T> void sort (list<t> List, comparator<? Super T> C)
Detailed examples of "comparable and comparator analysis"
5. Shuffle
public static void Shuffle (List<?> List)
public static void Shuffle (LIST<?
> list, Random rnd)
Mixed rows. The blending algorithm does exactly the opposite of sort: it disrupts any traces that may be in a list, regardless of the arrangement. That is, a random source-based input rearranges the list, which has the same probability (assuming that the random source is fair). This algorithm is very useful in implementing a game of chance.
Case 5-1:
PublicStaticvoidTest3 () {List<String> List = NewArrayList<>();List.Add"S1");List.Add"S2");List.Add"S3");List.Add"S4"); Collections.ShuffleList); System.Out.printlnList); }
Each execution result is different.
6. Reversal
There are three main ways of reversing this:
- public static void reverse (LIST<?
> list) directly reverses the elements of the collection
- public static <T> comparator<t> Reverseorder (); Returns a Comparator that enables the collection to be reversed
- public static <T> comparator<t> Reverseorder (comparator<t> cmp). Assuming that CMP is not NULL, returns the inverse of the CMP. That is, the reversal of the set reversal, and the last is no reversal. Assuming that CMP is NULL, the effect is equivalent to the second method.
Take a few examples to illustrate how to use the method.
Case 6-1:
List<String> List = NewArrayList<>();List.Add"S1");List.Add"S2");List.Add"S3");List.Add"S4"); System.Out.printlnList); Collections.ReverseList); System.Out.printlnList);
Execution Result:
[s1, s2, s3, s4][s4, s3, s2, s1]
Case 6-2:
List<String> List = NewArrayList<>();List.Add"S1");List.Add"S2");List.Add"S3"); System.Out.printlnList); Comparator<String>Comp=Collections.Reverseorder (); Collections.SortList, comp); System.Out.printlnList);
Results of implementation: (IBID.)
Case 6-3:
List<String> List = NewArrayList<>();List.Add"S1");List.Add"S2");List.Add"S3");List.Add"S4"); System.Out.printlnList); Comparator<String>Comp=Collections.Reverseorder (); Collections.SortList, comp); System.Out.printlnList); Comparator<String>Comp2=Collections.Reverseorder (comp); Collections.SortList, COMP2); System.Out.printlnList);
Execution Result:
[s1, s2, s3, s4][s4, s3, s2, s1][s1, s2, s3, s4]
Suppose comparator<string> comp2 = Collections.reverseorder (comp); change to comparator<string> COMP2 = Collections.reverseorder (null); execution Result:
[s1, s2, s3, s4][s4, s3, s2, s1][s4, s3, s2, s1]
7.synchronized Series
Ensure encapsulated Collection thread safety (strong sync)
- public static <T> collection<t> synchronizedcollection (collection<t> c)
- public static <T> sortedset<t> Synchronizedsortedset (sortedset<t> s)
- public static <T> list<t> synchronizedlist (list<t> List)
- public static <K,V> map<k,v> Synchronizedmap (map<k,v> m)
- public static <K,V> sortedmap<k,v> Synchronizedsortedmap (sortedmap<k,v> m)
- public static <T> sortedset<t> Synchronizedsortedset (sortedset<t> s)
8. Unmodifiable Series
Ensure that the encapsulated collection cannot be changed.
- public static <T> collection<t> unmodifiablecollection (collection<? extends t> c)
- public static <T> set<t> unmodifiableset (set<? extends t> s)
- public static <T> sortedset<t> Unmodifiablesortedset (sortedset<t> s)
- public static <T> list<t> unmodifiablelist (list<? extends t> List)
- public static <K,V> map<k,v> unmodifiablemap (map<? extends K,? extends v> m)
- public static <K,V> sortedmap<k,v> unmodifiablesortedmap (sortedmap<k,?
Extends v> m)
9. Other
The Collections tool class features more than these, as well as methods such as fill (), rotate (), Max (), Min (), and so on. It is not possible to remember all of a sudden, use the time to check the API.
References:
1. The analysis of comparable and comparator
Java Collection Framework: Collections Tool Class