Home> Java> javaTutorial> body text

Summary of Java-collections usage code examples

黄舟
Release: 2017-03-15 11:57:48
Original
1382 people have browsed it

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
Copy after login

Commonly used methods in Collections:

( 1)sort() sorting method

FunctionDefinition:publicstatic extends Comparable superT>> voidsort(List list) rootSort the specified list in ascending order according to the natural order of the elements.

Parameters: The list to be sorted.Function definition:

public static void sort(List list,Comparator 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 int binarySearch (List > list,T key)

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 int binarySearch(List list, T key,Comparator 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 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

(4)shuffle() shuffling method

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

## Function

numberDefinition: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 void fill(List 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 void copy(List dest,List 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(Collection coll), returns the smallest element of the given Collection according to the natural order of the elements. All elements in the Collection must implement ComparableInterface, 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 T min(Collection coll,Comparator 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(Collection 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 T max(Collection< ?extends T> coll,Comparator 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 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 boolean replaceAll(List list, T oldVal,TnewVal),使用另一个值替换列表总出现的所有的某一指定值。

参数: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还有很多其他的方法,如下表:


s, Class type) Enumeration c) T > T natural order boolean list, T oldVal, T newVal) s) ##static ##static binarySearch ##static ##static checkedMap ##static (Set ##static copy ##static boolean ##static # #emptySet ##static (Collection static int ##lastIndexOfSubList ##static max ##static returned the smallest element of the given collection according to their. ##static ##static newSetFromMap ##static (List ##static ##static reverseOrder ##static void ##static void #shuffle ##static Set List Returns an immutable list containing only the specified objects. ##static ##static void (List< ;?> list, int i, int j) ##static ##static (Set ##static unmodifiableCollection ##static List (List list) ##static unmodifiableSortedMap ##static SortedSet ## Return to a specified unmodified view of orderly SET.
MethodSummary
## static boolean addAll(Collection c, T...elements)Add all specified elements to the specified collection.
##static Queue asLifoQueue(Deque deque) Return theview
of a certain Deque in the form of last-in-first-out (Lifo) Queue.
int binarySearch(List > list, T key) Use binary search method to search the specified list to obtain the specified object.
int (List list, T key, Comparator c)Use binary search to search the specified list to obtain the specified object.
##static Collection # #checkedCollection(Collection c, Class type) can be returned to a dynamically typedsafe
view of the specified collection.
List checkedList(List list, Class type) Returns a dynamic type-safe view of the specified list.
Map(Map m, Class keyType, Class valueType) Returns a dynamic type-safe view of the specified mapping.
Set checkedSetReturns a dynamically type-safe view of the specified set.
##static SortedMap checkedSortedMap(SortedMap m, Class keyType, Class valueType) Returns a dynamic type-safe view of the specified ordered mapping.
##static SortedSet # #checkedSortedSet(SortedSet s, Class type) Returns a dynamic type-safe view of the specified ordered set.
void (List dest, List src)Copies all elements from one list to another.
##disjoint(Collection< ;?> c1, Collection c2) If there are no identical elements in the two specified collections,trueis returned.
##static List emptyList()Returns an empty list (immutable).
##static Map emptyMap() Returns an empty map (immutable).
Set()Returns an empty set (immutable).
enumerationReturns an enumeration over the specified collection.
##static void fill(List 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.
(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 ArrayList list(Enumeration e)can be returned.
> T (Collection According to the element of the Natural Sequence , 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.
##static
min(Collection coll)
##static T ##min(Collection According to the order generated by the specified comparator, return the minimum element of given together.
List nCopies(int n, T o) Returns an immutable list consisting of ncopies of the specified object.
Set (Map map) Returns the set supported by the specified mapping.
replaceAllReplaces 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.
Comparator reverseOrder() Returns a comparator that forcibly reverses the natural orderof a collection of objects that implements the
Comparableinterface.
Comparator (Comparator cmp) Returns a comparator that forcibly reverses the order of the specified comparator.
#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.
(List< ;?> list, Random rnd)Use the specified random source to replace the specified list.
# #singleton(T o) Returns an immutable set containing only the specified object.
##static
singletonList(T o)
##static Map singletonMap(K key, V value)Returns an immutable map that only maps the specified key to the specified value.
##static> void sort(List list) Sort the specified list in ascending order according to thenatural order
of the elements.
void sort(List list, Comparator c) Sorts the specified list according to the order produced by the specified comparator.
##swapwill swap elements at the specified positions in the specified list.
##static Collection synchronizedCollection(Collection c)Returns a synchronized (thread-safe) collection supported by the specified collection.
##static List synchronizedList(List list) Returns a synchronized (thread-safe) list supported by the specified list.
Map synchronizedMap(Map m) Returns a synchronized (thread-safe) map backed by the specified map.
Set# #synchronizedSetReturns a synchronous (thread-safe) set supported by the specified set.
##static SortedMap synchronizedSortedMap(SortedMap m)returned.
##static SortedSet # #synchronizedSortedSet(SortedSet s) Returns a synchronized (thread-safe) ordered set supported by the specified ordered set.
Collection (Collection c)Returns an unmodifiable view of the specified collection.
unmodifiableListReturns an unmodifiable view of the specified list.
##static Map unmodifiableMap(MapReturn to the non -modified view of specified mapping.
##static Set # #unmodifiableSet(Set s) Returns the unmodifiable view of the specified set.
SortedMap(SortedMap m) Returns an unmodifiable view of the specified ordered map.
# #UnmodiFiablesortedSet(SortedSet & LT; T & GT; S)

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!