Java.lang.Collections
This class consists entirely of static methods that operate on collection or return collection. This means that collections provides some methods for collection collection operations. There are several other examples of JDK like this: array and Arrays,object and objects.
The collections class has no subclasses and the constructor is private, which causes all its external methods to be static.
Main methods
Two forms are available:
public static <t extends Comparable<? Super t>> void Sort (list<t> List)
public static <T> void sort (list<t> List, comparator<? Super T> C)
Description
1. Two ways to differentiate is one more parameter comparator, when the first method is used, comparator defaults to NULL
2.if (legacymergesort.userrequested) Legacymergesort (a); if userrequested has a specified value, use Legacymergesort, otherwise, sort with timsort
The 3.sort () method uses the Timsort sorting algorithm at the bottom. if c = = null (using the first form or the second form is null), Comparabletimsort.sort (A, 0, a.length, NULL, 0, 0) are used, otherwise the Timsort sort algorithm is used.
4.TimSort sorting is an evolutionary version of the merge sort, or, more specifically, a combination of merge sort and insert sort. Its time complexity is generally less than O (NLG2 (n))
- BinarySearch: Two-point search
Two forms are available:
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)
Description
1, the difference between the two ways is also a parameter comparator.
2, two-point lookup requires a sorted list is ordered and the elements can be compared. This is list< from the list parameter;? Extends Comparable<? Super t>> can be seen.
3. Two ways to find by default are available:
A. Indexedbinarysearch: A dichotomy based on subscript
B. Iteratorbinarysearch: Iterator dichotomy
The conditions for using both are: If the specified list does not implement the Randomaccess interface and is a large list, select Indexedbinarysearch, otherwise select Iteratorbinarysearch
i.e.: if (list instanceof randomaccess | | list.size () <binarysearch_threshold)
4. The use of the displacement operator >>> can be used for reference in the dichotomy of intermediate node subscript.
It's a good idea to reverse the order of the elements in the collection.
Can be understood as shuffle action, the effect is to set the elements of the confusion, the realization is the use of random numbers
Replaces all elements in the specified list with the specified element.
function form:
public static void Rotate (list<?> List, int distance)
It can be understood that the elements in the set are understood as multiple points in a circle, where the rotate operation can be either a forward displacement or a reverse displacement, and the order of all elements after the displacement is constant and the position is changed.
Example:
The collection list contains elements [A,b,c,d,e], and if distance=2 is specified, the method is called
Collections.rotate (list,2);
, the order of the elements in the collection list is: [D,e,a,b,c]. That is, the overall positive movement of the collection element is two positions. Note that this method is useful for sub-lists, and you can move one or more elements in the list while preserving the order of the remaining elements. For the above example call:collections.rotate (list.substring (1,4), 2);
The resulting list is: [A,d,c,b,e]
Analysis: list.substring (1,4) will get a list of [b,c,d], then [b,c,d] move two units, and the original set in the position of a and e unchanged. So the results are as shown above.
- Indexofsublist: Where the string appears
That is, the position of the first occurrence of a string in the original string subscript.
- Empty_set empty_list Empty_map
Generally as the entrance of the function to determine if the parameter is empty, the corresponding collection type is returned directly. Instead of returning null. For example:
Public List get (list<string> list) { if(List.isEmpty ()) { return collections.empty_list; } ... ... }
Summarize
1, for the operation of the collection, you can give priority to using the method provided by collections
2, for the string operation, if there is a set of more appropriate functions, you can first convert the string into a collection, and further call the corresponding method in the collection. String to collection can use the splitter class provided by guava
Java.lang.Collections