Java Collection Framework: Collections Tool Class

Source: Internet
Author: User
Tags addall comparable shuffle

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:

    1. public static void reverse (list<?> List) directly reverses the elements of the collection
    2. public static <T> comparator<t> Reverseorder (); Returns a comparer that enables the collection to be reversed Comparator
    3. 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)

    1. public static <T> collection<t> synchronizedcollection (collection<t> c)
    2. public static <T> sortedset<t> Synchronizedsortedset (sortedset<t> s)
    3. public static <T> list<t> synchronizedlist (list<t> List)
    4. public static <K,V> map<k,v> Synchronizedmap (map<k,v> m)
    5. public static <K,V> sortedmap<k,v> Synchronizedsortedmap (sortedmap<k,v> m)
    6. public static <T> sortedset<t> Synchronizedsortedset (sortedset<t> s)
8. Unmodifiable Series

Ensure that the encapsulated collection cannot be modified.

    1. public static <T> collection<t> unmodifiablecollection (collection<? extends t> c)
    2. public static <T> set<t> unmodifiableset (set<? extends t> s)
    3. public static <T> sortedset<t> Unmodifiablesortedset (sortedset<t> s)
    4. public static <T> list<t> unmodifiablelist (list<? extends t> List)
    5. public static <K,V> map<k,v> unmodifiablemap (map<? extends K,? extends v> m)
    6. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.