What you see on paper is shallow, but you know you have to do it in detail --Lu YouAsk the canal how clear it is to have a source of living water --Zhu Xi
# Class Collections is a Packaging category. It contains variousstaticmultistate methodsrelated to collection operations. This classcannot be instantiated, just like atool classthat serves Java's Collectionframework.
java.lang.Object java.util.Collections
Commonly used methods in Collections:
( 1)sort() sorting method
FunctionDefinition:publicstatic
Parameters: The list to be sorted.Function definition:
public static
Parameters: list-the list to be sorted; c-the comparator that determines the order of the list.(2) binarySearch()
Binary searchMethod
Function definition: public static
Use the binarysearchmethod to search the specified list to obtain the specifiedobject. Before calling this method, the list elements must be sorted in ascending order, otherwise the result is uncertain , this method will perform O(n) link traversals and O(log n) element comparisons.
Parameters: list-the linked list to be searched, key-the key to be searched.
Function definition:public static
Parameters: list-the list to be searched, key-the key to be searched, c-the comparator of the sorted list.
(3) reverse() reverse method
#Number definition:public static voidreverse(List list), reverses the order of elements in the specified list. This method runs in linear time.
Parameters: list-the list whose elements are to be reversed
Functionnumber definition:public static voidshuffle(List list) uses the default random source to replace the specified list. The probability of all replacements occurring is approximately equal.
Parameters: list-the list to be reorganized
## FunctionnumberDefinition:public static void shuffle(List list,Random rnd), uses the specified random source to replace the specified list.
Parameters: list-the list to be shuffled, rnd-the random source used to shuffle the list.(5) swap() exchange method
Function definition:public static voidswap(List list,int i,int j), exchange elements at the specified position in the specified list.
Parameters: list-the list for element exchange, i-theindexof one element to be exchanged, j-the other element to be exchanged The index of an element.
(6) fill() replacement method
Function definition:public static
Parameters: list - a list filled with specified elements, obj - the elements used to fill the specified list.
(7)copy()Copy method
Function definition:public static
Parameters: dest-dest list, src-source list.
(8) min() minimum value method
Function definition:public static
Parameters: coll - the collection whose smallest element will be determined.
Function definition:public static
Parameters: coll-the collection whose minimum element will be determined, comp-the comparator used to determine the minimum element.
(9) max() maximum value method
Function definition:public static
Parameters: coll - the collection whose largest element will be determined.
Function definition:public static
Parameters: coll-the collection whose largest element will be determined, comp-the comparator used to determine the largest element
(10)rotate() rotation method
Function definition:public static voidrotate(List list,int distance), rotates the elements in the specified list according to the specified distance.
Parameters: list-the list to be rotated, distance-the distance for list rotation, which can be 0, a negative number, or a number greater than list.size().
(11)replaceAll()Replace all functions
函数定义:public static
参数:list-在其中进行替换的列表;oldVal-将被替换的原值;newVal-替换oldVald的新值。
示例代码:
public class Hello { public static void main(String[] args) { System.out.println("sort"); List list=new ArrayList(); double array[] = {112, 111, 23, 456, 231 }; for (int i = 0; i < array.length; i++) { list.add(new Double(array[i])); } Collections.sort(list);//自然排序 for (int i = 0; i < array.length; i++) { System.out.println(list.get(i)); } System.out.println("shuffle"); Collections.shuffle(list);//置换 for (int i = 0; i < array.length; i++) { System.out.println(list.get(i)); } Collections.sort(list);//自然排序 System.out.println("reverse"); Collections. reverse (list);//反转 for (int i = 0; i < array.length; i++) { System.out.println(list.get(i)); } Collections.sort(list);//自然排序 System.out.println("copy"); List li = new ArrayList(); double arr[] = {1131,333}; for(int j=0;j Copy after login
以上是Collections比较常用的方法,Collections还有很多其他的方法,如下表:
MethodSummary | |
---|---|
## static
|
addAll(Collection super T> c, T...elements) Add all specified elements to the specified collection. |
##static
|
asLifoQueue(Deque viewof a certain Deque in the form of last-in-first-out (Lifo) Queue. |
|
binarySearch(List
> list, T key) Use binary search method to search the specified list to obtain the specified object. |
| binarySearch
(List
list, T key, Comparator
c)Use binary search to search the specified list to obtain the specified object. |
##static
|
# #checkedCollection(Collection safeview of the specified collection. |
|
checkedList(List |
| checkedMap
(MapReturns a dynamic type-safe view of the specified mapping. | ##static
Set
| (Set
Returns a dynamically type-safe view of the specified set. |
##static
|
checkedSortedMap(SortedMap Returns a dynamic type-safe view of the specified ordered mapping. |
##static
|
# #checkedSortedSet(SortedSet
Returns a dynamic type-safe view of the specified ordered set.
|
| copy
(List
dest, List
src)Copies all elements from one list to another. | ##static boolean
##disjoint(Collection< ;?> c1, Collection
c2) |
If there are no identical elements in the two specified collections,true is returned. |
##static
|
emptyList() Returns an empty list (immutable). |
##static
|
emptyMap() Returns an empty map (immutable). |
| # #emptySet
()Returns an empty set (immutable). | ##static
enumeration | (Collection
Returns an enumeration over the specified collection. |
##static
|
fill(List super T> list, T obj) Replaces all elements in the specified list with the specified element. |
static int
|
##frequency(Collection< ;?> c, Object o) Returns the number of elements in the specified collection that are equal to the specified object. |
static int
|
##indexOfSubList(List< ;?> source, List
target) out out can be returned. |
| ##lastIndexOfSubList
(List< ;?> source, List
target)Returns the starting position of the last occurrence of the specified target list in the specified source list; if no such list appears, returns -1. |
##static
|
list(Enumeration can be returned. |
| max
(Collection
According to the element of the Natural Sequence , return to the greatest element of given by given. | ##static
##max(Collection
|
According to the order generated by the specified comparator, return the greatest element of given collection.
##static |
min (Collection
coll) | returned the smallest element of the given collection according to their.
|
##static
|
##min(Collection extends T & GT; color, comparator & lt ;? Super T & GT; Comp) According to the order generated by the specified comparator, return the minimum element of given together. |
|
nCopies(int n, T o) Returns an immutable list consisting of n copies of the specified object.
|
| newSetFromMap
(Map
Returns the set supported by the specified mapping. | ##static
replaceAll | (List
Replaces all occurrences of a specified value in a list with another value. |
##static void
|
##reverse(List< ;?> list) Reverse the order of elements in the specified list. |
|
reverseOrder() Returns a comparator that forcibly reverses the natural order of a collection of objects that implements theComparable interface. |
| reverseOrder
(Comparator
Returns a comparator that forcibly reverses the order of the specified comparator. | ##static void
#rotate(List< ;?> list, int distance) |
Rotates the elements in the specified list according to the specified distance. |
##static void
|
#shuffle(List< ;?> list) Use the default random source to replace the specified list. |
| #shuffle
(List< ;?> list, Random rnd)Use the specified random source to replace the specified list. | ##static
# #singleton(T o) |
Returns an immutable set containing only the specified object.
##static |
singletonList (T o) | Returns an immutable list containing only the specified objects.
|
##static
|
singletonMap(K key, V value) Returns an immutable map that only maps the specified key to the specified value. |
##static
|
sort(List natural orderof the elements. |
|
sort(List
Sorts the specified list according to the order produced by the specified comparator.
|
##swap | (List< ;?> list, int i, int j)
will swap elements at the specified positions in the specified list. |
##static
|
synchronizedCollection(Collection Returns a synchronized (thread-safe) collection supported by the specified collection. |
##static
|
synchronizedList(List |
|
synchronizedMap(Map Returns a synchronized (thread-safe) map backed by the specified map. |
# #synchronizedSet | (Set
Returns a synchronous (thread-safe) set supported by the specified set. |
##static
|
synchronizedSortedMap(SortedMap returned. |
##static
|
# #synchronizedSortedSet(SortedSet
Returns a synchronized (thread-safe) ordered set supported by the specified ordered set.
|
| unmodifiableCollection
(Collection
c)Returns an unmodifiable view of the specified collection. | ##static
unmodifiableList | (List
list)
Returns an unmodifiable view of the specified list. |
##static
|
unmodifiableMap(Map Extends K ,? Extends v & GT; M) Return to the non -modified view of specified mapping. |
##static
|
# #unmodifiableSet(Set
s) Returns the unmodifiable view of the specified set. |
| unmodifiableSortedMap
(SortedMap
Returns an unmodifiable view of the specified ordered map. | ##static
# #UnmodiFiablesortedSet(SortedSet & LT; T & GT; S) | ## Return to a specified unmodified view of orderly SET.
|
The above is the detailed content of Summary of Java-collections usage code examples. For more information, please follow other related articles on the PHP Chinese website!