The Java.util.Collections tool class provides a number of useful methods that make it easier and more convenient for the program ape to manipulate the collection class, and these methods are static. The entire Collections tool class source code is almost 4000 lines, we have some typical methods to illustrate.
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 certain elements to the specified set 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);
Operation 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 some of the previous Liebovin friends should be very clear, the specific can also refer to the "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)
Specific examples can refer to "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 the traces of any permutation that may be in a list. That is, a random source-based input rearranges the list, and such permutations have 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 run has a different result.
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 comparer that enables the collection to be reversed Comparator
- public static <T> comparator<t> Reverseorder (comparator<t> cmp); If CMP is not NULL, returns the inverse comparer for CMP, That is, the reversal of the set reversal, and the last is no reversal. If CMP is NULL, the effect is equivalent to the second method.
Give some examples to illustrate the usage.
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);
Operation 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);
Operation Result: (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);
Operation Result:
[s1, s2, s3, s4][s4, s3, s2, s1][s1, s2, s3, s4]
If you put comparator<string> comp2 = Collections.reverseorder (comp), change to comparator<string> COMP2 = Collections.reverseorder (null); Run 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 modified.
- 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 fill (), rotate (), Max (), Min () and other methods, it is impossible to remember all of a sudden, when using the API can be more check.
Resources:
1. The analysis of comparable and comparator
Java Collection Framework: Collections Tool Class