Home  >  Article  >  Java  >  Summary of Java-collections usage code examples

Summary of Java-collections usage code examples

黄舟
黄舟Original
2017-03-15 11:57:481394browse

What you see on paper is shallow, but you know you have to do it in detail --Lu You Ask the canal how clear it is to have a source of living water --Zhu Xi


# Class Collections is a Packaging category. It contains various staticmultistate methods related to collection operations. This class cannot be instantiated, just like a tool class that serves Java's Collection framework.

java.lang.Object
        java.util.Collections

Commonly used methods in Collections:

( 1) sort() sorting method

FunctionDefinition: public static 611fd305ebaea40c489374f3a6a2a96c> void sort(List8742468051c85b06f0a0af9e3e506b5c list) root Sort the specified list in ascending order according to the natural order of the elements.

Parameters: The list to be sorted. Function definition:

public static 8742468051c85b06f0a0af9e3e506b5c void sort(List8742468051c85b06f0a0af9e3e506b5c list,Comparator117c5a0bdb71ea9a9d0c2b99b03abe3e c), generated based on the specified comparator Sorts the specified list in order. All elements within this list must be comparable to each other using the specified comparator.

Parameters: list-the list to be sorted; c-the comparator that determines the order of the list. (2) binarySearch()

Binary search

Method

Function definition: public static 8742468051c85b06f0a0af9e3e506b5c int binarySearch (List list,T key)

Use the binary search method to search the specified list to obtain the specified object. 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 8742468051c85b06f0a0af9e3e506b5c int binarySearch(Listd203bb1ae585225d4838a2b7e3d0503e list, T key, Comparator117c5a0bdb71ea9a9d0c2b99b03abe3e c) Sort the list in ascending order according to the specified comparator.

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 void reverse(List6b3d0130bba23ae47fe2b8e8cddf0195 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

(4) shuffle() shuffling method

Functionnumber definition:public static void shuffle(List6b3d0130bba23ae47fe2b8e8cddf0195 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

## Function

numberDefinition:public static void shuffle(List6b3d0130bba23ae47fe2b8e8cddf0195 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 void swap(List6b3d0130bba23ae47fe2b8e8cddf0195 list,int i, int j), exchange elements at the specified position in the specified list.

Parameters: list-the list for element exchange, i-the index of 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 8742468051c85b06f0a0af9e3e506b5c void fill(List2c7dc8671179272755aec1f6022876ad list,T obj), replaces all elements in the specified list with the specified element, running in linear time.

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 8742468051c85b06f0a0af9e3e506b5c void copy(List2c7dc8671179272755aec1f6022876ad dest,Listd203bb1ae585225d4838a2b7e3d0503e src), copies all elements from one list to another. After doing this, the index of each copied element in the destination list will be equal to the index of that element in the source list, and the destination list must be at least as long as the source list.

Parameters: dest-dest list, src-source list.

(8) min() minimum value method

Function definition: public static T min(Collectiond203bb1ae585225d4838a2b7e3d0503e coll), returns the smallest element of the given Collection according to the natural order of the elements. All elements in the Collection must implement Comparable Interface, In addition, all elements in the collection must be comparable to each other.

Parameters: coll - the collection whose smallest element will be determined.

Function definition: public static 8742468051c85b06f0a0af9e3e506b5c T min(Collection6f577ce863ae620b571540d817dd4482 coll,Comparator117c5a0bdb71ea9a9d0c2b99b03abe3e comp), returns the smallest element of the given collection according to the order generated by the specified comparator.

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 T max(Collectiond203bb1ae585225d4838a2b7e3d0503e coll), returns the largest element of the given collection according to the natural order of the elements.

Parameters: coll - the collection whose largest element will be determined.

Function definition: public static 8742468051c85b06f0a0af9e3e506b5c T max(Collectionb0321d643ef24b7fbf4f3031ca3ebe7e coll,Comparator117c5a0bdb71ea9a9d0c2b99b03abe3e comp), returns the largest element of the given collection according to the order generated by the specified comparator.

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 void rotate(List6b3d0130bba23ae47fe2b8e8cddf0195 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 8742468051c85b06f0a0af9e3e506b5c boolean replaceAll(List8742468051c85b06f0a0af9e3e506b5c list,T oldVal,newVal),使用另一个值替换列表总出现的所有的某一指定值。

        参数:list-在其中进行替换的列表;oldVal-将被替换的原值;newVal-替换oldVald的新值。


示例代码:

public class Hello {
public static void main(String[] args) {
        System.out.println("sort");
        List list=new ArrayList<Double>();
       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<arr.length;j++){
            li.add(new Double(arr[j]));
        }
        Collections.copy(list,li);//拷贝
        for (int i = 0; i <list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println("min");
        System.out.println(Collections.min(list));//返回最小值
        System.out.println("max");
        System.out.println(Collections.max(list));//返回最大值
        System.out.println("rotate");
        Collections.rotate(list,-1);//循环
        for (int i = 0; i <list.size(); i++) {
            System.out.println( list.get(i));
        }
         System.out.println("binarySearch");
        Collections.sort(list);
        System.out.println(list);
        System.out.println(Collections.binarySearch(list, 333.0));//二分查找
    }
}

以上是Collections比较常用的方法,Collections还有很多其他的方法,如下表:


##static##staticbinarySearch##static##staticcheckedMap##static1a4db2c2c2313771e5742b6debf617a1 (Set1a4db2c2c2313771e5742b6debf617a1 s, Class1a4db2c2c2313771e5742b6debf617a1 type)##staticcopy##static boolean##static# #emptySet##static8742468051c85b06f0a0af9e3e506b5c Enumeration8742468051c85b06f0a0af9e3e506b5c(Collection8742468051c85b06f0a0af9e3e506b5c c)static int##lastIndexOfSubList##staticmax##static8742468051c85b06f0a0af9e3e506b5c T T                                                                                                                                                                                                                                                                          returned the smallest element of the given collection according to their natural order. ##static##staticnewSetFromMap##static8742468051c85b06f0a0af9e3e506b5c boolean(List8742468051c85b06f0a0af9e3e506b5c list, T oldVal, T newVal)##static##staticreverseOrder##static void##static void#shuffle##static8742468051c85b06f0a0af9e3e506b5c Set8742468051c85b06f0a0af9e3e506b5c8742468051c85b06f0a0af9e3e506b5c List8742468051c85b06f0a0af9e3e506b5c                Returns an immutable list containing only the specified objects. ##static##static void(List< ;?> list, int i, int j)##static##static(Set8742468051c85b06f0a0af9e3e506b5c s)##staticunmodifiableCollection##static8742468051c85b06f0a0af9e3e506b5c List8742468051c85b06f0a0af9e3e506b5c(List6f577ce863ae620b571540d817dd4482 list)##staticunmodifiableSortedMap##static8742468051c85b06f0a0af9e3e506b5c SortedSet8742468051c85b06f0a0af9e3e506b5c ## Return to a specified unmodified view of orderly SET.
MethodSummary
## static8742468051c85b06f0a0af9e3e506b5c boolean addAll<span style="background-color:inherit; color:rgb(51,51,51)">(Collection2c7dc8671179272755aec1f6022876ad c, T...elements)</span> Add all specified elements to the specified collection.
##static8742468051c85b06f0a0af9e3e506b5c Queue8742468051c85b06f0a0af9e3e506b5c asLif<span style="background-color:inherit; color:rgb(51,51,51)">oQueue<a href="http://www.php.cn/wiki/109.html" target="_blank">(Deque8742468051c85b06f0a0af9e3e506b5c deque)</a> </span>                                                                                                            Return the view
of a certain Deque in the form of last-in-first-out (Lifo) Queue.
8742468051c85b06f0a0af9e3e506b5c int binarySearch(List list, T key) <span style="background-color:inherit; color:rgb(51,51,51)"> Use binary search method to search the specified list to obtain the specified object. </span>
8742468051c85b06f0a0af9e3e506b5c int (List6f577ce863ae620b571540d817dd4482 list, T key, Comparator117c5a0bdb71ea9a9d0c2b99b03abe3e c) Use binary search to search the specified list to obtain the specified object.
##static1a4db2c2c2313771e5742b6debf617a1 Collection1a4db2c2c2313771e5742b6debf617a1 # #checkedCollection(Collection1a4db2c2c2313771e5742b6debf617a1 c, Class1a4db2c2c2313771e5742b6debf617a1 type)<span style="background-color:inherit; color:rgb(51,51,51)"> </span>                                                                                                                                                                                                  can be returned to a dynamically typed safe
view of the specified collection.
1a4db2c2c2313771e5742b6debf617a1 List1a4db2c2c2313771e5742b6debf617a1 checkedList<span style="background-color:inherit; color:rgb(51,51,51)">(List1a4db2c2c2313771e5742b6debf617a1 list, Class1a4db2c2c2313771e5742b6debf617a1 type)<a href="http://www.php.cn/wiki/596.html" target="_blank"> </a> Returns a dynamic type-safe view of the specified list. </span>
b77a8d9c3c319e50d4b02a976b347910 Mapb77a8d9c3c319e50d4b02a976b347910 (Mapb77a8d9c3c319e50d4b02a976b347910 m, Class245c3adc26563b673f7297c0b3777639 keyType, Classd94943c0b4933ad8cac500132f64757f valueType)                                                                                                                                                                             Returns a dynamic type-safe view of the specified mapping. <span style="background-color:inherit; color:rgb(51,51,51)"></span>
Set1a4db2c2c2313771e5742b6debf617a1checkedSet                 Returns a dynamically type-safe view of the specified set.
##staticb77a8d9c3c319e50d4b02a976b347910 SortedMapb77a8d9c3c319e50d4b02a976b347910 checkedSortedMap<span style="background-color:inherit; color:rgb(51,51,51)">(SortedMapb77a8d9c3c319e50d4b02a976b347910 m, Class245c3adc26563b673f7297c0b3777639 keyType, Classd94943c0b4933ad8cac500132f64757f valueType)</span>                             Returns a dynamic type-safe view of the specified ordered mapping.
##static1a4db2c2c2313771e5742b6debf617a1 SortedSet1a4db2c2c2313771e5742b6debf617a1 # #checkedSortedSet(SortedSet1a4db2c2c2313771e5742b6debf617a1 s, Class1a4db2c2c2313771e5742b6debf617a1 type) <span style="background-color:inherit; color:rgb(51,51,51)">                                                     Returns a dynamic type-safe view of the specified ordered set. </span>
8742468051c85b06f0a0af9e3e506b5c void (List117c5a0bdb71ea9a9d0c2b99b03abe3e dest, Listd203bb1ae585225d4838a2b7e3d0503e src) Copies all elements from one list to another. <span style="background-color:inherit; color:rgb(51,51,51)"></span>
##disjoint(Collection< ;?> c1, Collection6b3d0130bba23ae47fe2b8e8cddf0195 c2) If there are no identical elements in the two specified collections, true is returned.
##static8742468051c85b06f0a0af9e3e506b5c List8742468051c85b06f0a0af9e3e506b5c emptyList<span style="background-color:inherit; color:rgb(51,51,51)">()</span> Returns an empty list (immutable).
##staticb77a8d9c3c319e50d4b02a976b347910 Mapb77a8d9c3c319e50d4b02a976b347910 emptyMap()<span style="background-color:inherit; color:rgb(51,51,51)"> </span> Returns an empty map (immutable).
8742468051c85b06f0a0af9e3e506b5c Set8742468051c85b06f0a0af9e3e506b5c() Returns an empty set (immutable). <span style="background-color:inherit; color:rgb(51,51,51)"></span>
enumeration                Returns an enumeration over the specified collection.
##static8742468051c85b06f0a0af9e3e506b5c void fill<span style="background-color:inherit; color:rgb(51,51,51)">(List117c5a0bdb71ea9a9d0c2b99b03abe3e list, T obj)</span> Replaces all elements in the specified list with the specified element.
static int ##frequency(Collection&lt ;?> c, Object o)<span style="background-color:inherit; color:rgb(51,51,51)"> </span> Returns the number of elements in the specified collection that are equal to the specified object.
static int ##indexOfSubList(List< ;?> source, List6b3d0130bba23ae47fe2b8e8cddf0195 target) <span style="background-color:inherit; color:rgb(51,51,51)">                                                                                                                                                                                                                                                                                out out can be returned. </span>
(List< ;?> source, List6b3d0130bba23ae47fe2b8e8cddf0195 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.
##static8742468051c85b06f0a0af9e3e506b5c ArrayList8742468051c85b06f0a0af9e3e506b5c list<span style="background-color:inherit; color:rgb(51,51,51)">(Enumeration8742468051c85b06f0a0af9e3e506b5c e)</span>                                                                                                           can be returned.
T (Collection According to the element of the <span style="background-color:inherit; color:rgb(51,51,51)"> Natural Sequence </span>, return to the greatest element of given by given.
##max(Collection According to the order generated by the specified comparator, return the greatest element of given collection. <span style="background-color:inherit; color:rgb(51,51,51)"></span>
##static
min(Collectionc04165aef787a17e51f68c2c6a2730fc coll)
##static8742468051c85b06f0a0af9e3e506b5c T ##min(Collection<? extends T & GT; color, comparator & lt ;? Super T & GT; Comp) <span style="background-color:inherit; color:rgb(51,51,51)"> </span> According to the order generated by the specified comparator, return the minimum element of given together.
8742468051c85b06f0a0af9e3e506b5c List8742468051c85b06f0a0af9e3e506b5c nCopies(int n, T o) <span style="background-color:inherit; color:rgb(51,51,51)">          Returns an immutable list consisting of </span>n copies of the specified object.
1a4db2c2c2313771e5742b6debf617a1 Set1a4db2c2c2313771e5742b6debf617a1 (Map149d50d9cfbc48a869df7dfd8cfeb54e map)                         Returns the set supported by the specified mapping. <span style="background-color:inherit; color:rgb(51,51,51)"></span>
replaceAll Replaces all occurrences of a specified value in a list with another value.
##static void ##reverse(List&lt ;?> list)<span style="background-color:inherit; color:rgb(51,51,51)"> </span> Reverse the order of elements in the specified list.
8742468051c85b06f0a0af9e3e506b5c Comparator8742468051c85b06f0a0af9e3e506b5c reverseOrder() <span style="background-color:inherit; color:rgb(51,51,51)"> Returns a comparator that forcibly reverses the </span>natural order of a collection of objects that implements the
Comparable interface.
8742468051c85b06f0a0af9e3e506b5c Comparator8742468051c85b06f0a0af9e3e506b5c (Comparator8742468051c85b06f0a0af9e3e506b5c cmp) Returns a comparator that forcibly reverses the order of the specified comparator. <span style="background-color:inherit; color:rgb(51,51,51)"></span>
#rotate(List< ;?> list, int distance) Rotates the elements in the specified list according to the specified distance.
##static void #shuffle(List&lt ;?> list)<span style="background-color:inherit; color:rgb(51,51,51)"> </span> Use the default random source to replace the specified list.
(List< ;?> list, Random rnd) Use the specified random source to replace the specified list. <span style="background-color:inherit; color:rgb(51,51,51)"></span>
# #singleton(T o) Returns an immutable set containing only the specified object. <span style="background-color:inherit; color:rgb(51,51,51)"></span>
##static
singletonList(T o)
##staticb77a8d9c3c319e50d4b02a976b347910 Mapb77a8d9c3c319e50d4b02a976b347910 singletonMap<span style="background-color:inherit; color:rgb(51,51,51)">(K key, V value)</span> Returns an immutable map that only maps the specified key to the specified value.
##static<T extends Comparable117c5a0bdb71ea9a9d0c2b99b03abe3e> void sort(List8742468051c85b06f0a0af9e3e506b5c list)<span style="background-color:inherit; color:rgb(51,51,51)"> </span>         Sort the specified list in ascending order according to the natural order
of the elements.
8742468051c85b06f0a0af9e3e506b5c void sort(List8742468051c85b06f0a0af9e3e506b5c list, Comparator117c5a0bdb71ea9a9d0c2b99b03abe3e c) <span style="background-color:inherit; color:rgb(51,51,51)"> Sorts the specified list according to the order produced by the specified comparator. </span>
##swap                                                                                                                                                         will swap elements at the specified positions in the specified list.
##static8742468051c85b06f0a0af9e3e506b5c Collection8742468051c85b06f0a0af9e3e506b5c syn<span style="background-color:inherit; color:rgb(51,51,51)">chr<a href="http://www.php.cn/wiki/1332.html" target="_blank">onizedCollection</a>(Collection8742468051c85b06f0a0af9e3e506b5c c)</span>                                                                                                                                           Returns a synchronized (thread-safe) collection supported by the specified collection.
##static8742468051c85b06f0a0af9e3e506b5c List8742468051c85b06f0a0af9e3e506b5c synchronizedList(List8742468051c85b06f0a0af9e3e506b5c list)<span style="background-color:inherit; color:rgb(51,51,51)"> </span> Returns a synchronized (thread-safe) list supported by the specified list.
b77a8d9c3c319e50d4b02a976b347910 Mapb77a8d9c3c319e50d4b02a976b347910 synchronizedMap(Mapb77a8d9c3c319e50d4b02a976b347910 m) <span style="background-color:inherit; color:rgb(51,51,51)"> Returns a synchronized (thread-safe) map backed by the specified map. </span>
8742468051c85b06f0a0af9e3e506b5c Set8742468051c85b06f0a0af9e3e506b5c# #synchronizedSet                                           Returns a synchronous (thread-safe) set supported by the specified set.
##staticb77a8d9c3c319e50d4b02a976b347910 SortedMapb77a8d9c3c319e50d4b02a976b347910 synchronizedSortedMap<span style="background-color:inherit; color:rgb(51,51,51)">(SortedMapb77a8d9c3c319e50d4b02a976b347910 m)</span>                                                                                                                                                                                                                                                         returned.
##static8742468051c85b06f0a0af9e3e506b5c SortedSet8742468051c85b06f0a0af9e3e506b5c # #synchronizedSortedSet(SortedSet8742468051c85b06f0a0af9e3e506b5c s) <span style="background-color:inherit; color:rgb(51,51,51)">                                                                                                                                Returns a synchronized (thread-safe) ordered set supported by the specified ordered set. </span>
8742468051c85b06f0a0af9e3e506b5c Collection8742468051c85b06f0a0af9e3e506b5c (Collection6f577ce863ae620b571540d817dd4482 c) Returns an unmodifiable view of the specified collection. <span style="background-color:inherit; color:rgb(51,51,51)"></span>
unmodifiableList                Returns an unmodifiable view of the specified list.
##staticb77a8d9c3c319e50d4b02a976b347910 Mapb77a8d9c3c319e50d4b02a976b347910 unmodifiableMap<span style="background-color:inherit; color:rgb(51,51,51)">(Map<? Extends K ,? Extends v & GT; M) </span> Return to the non -modified view of specified mapping.
##static8742468051c85b06f0a0af9e3e506b5c Set8742468051c85b06f0a0af9e3e506b5c # #unmodifiableSet(Set6f577ce863ae620b571540d817dd4482 s) <span style="background-color:inherit; color:rgb(51,51,51)">                                                                                                                                                               Returns the unmodifiable view of the specified set. </span>
b77a8d9c3c319e50d4b02a976b347910 SortedMapb77a8d9c3c319e50d4b02a976b347910 (SortedMap73e86936beb908d627d782a3d5719990 m)                         Returns an unmodifiable view of the specified ordered map. <span style="background-color:inherit; color:rgb(51,51,51)"></span>
# #UnmodiFiablesortedSet (SortedSet & LT; T & GT; S) <span style="background-color:inherit; color:rgb(51,51,51)"></span>

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn