Java Collection Framework: Collections Tool Class

Source: Internet
Author: User
Tags addall comparable shuffle

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:

    1. public static void reverse (LIST<?

      > list) directly reverses the elements of the collection

    2. public static <T> comparator<t> Reverseorder (); Returns a Comparator that enables the collection to be reversed
    3. 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)

    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 changed.

    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 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

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.