* Tool classes for operating collection and map: Collections
--------------------------------------------------------------------------------------------------------------
* Reverse (List): Reverses the order of the elements in the list
Shuffle (list): Randomly sort the elements of a List collection
Sort (list): Sorts the specified List collection elements in ascending order based on the natural ordering of the elements
Sort (list,comparator): Sorts the List collection elements according to the order in which the specified Comparator is produced
Swap (list,int, int): Swaps the I and J elements in the specified list collection
1 @Test2 Public voidtestCollections1 () {3List List =NewArrayList ();4List.add (123);5List.add (456);6List.add (12);7List.add (78);8 System.out.println (list);9 collections.reverse (list);Ten System.out.println (list); One collections.shuffle (list); A System.out.println (list); - Collections.sort (list); - System.out.println (list); theCollections.swap (list, 0, 2); - System.out.println (list); -}
------------------------------------------------------------------------------------------------------
* Objectmax (Collection): Returns the largest element in a given set based on the natural order of the element
Objectmax (Collection,comparator): Returns the largest element in a given set, based on the order specified by Comparator
Objectmin (Collection)
Objectmin (Collection,comparator)
Intfrequency (Collection,object): Returns the number of occurrences of the specified element in the specified collection
Voidcopy (List dest,list src): Copy content from SRC to dest
Booleanreplaceall (list list,object oldval,object newval): Replaces all old values of the List object with the new value
1 @Test2 Public voidTestCollections2 () {3List List =NewArrayList ();4List.add (123);5List.add (456);6List.add (12);7List.add (78);8List.add (456);9Object obj =Collections.max (list);Ten System.out.println (obj); One intCount = collections.frequency (list, 4567); A System.out.println (count); - //implementing the copy of List - //List list1 = new ArrayList ();//How to implement the error theList List1 = Arrays.aslist (Newobject[list.size ()]); - collections.copy (List1, list); - System.out.println (list1); - //The list's thread safety is ensured by the following methods. +List List2 =collections.synchronizedlist (list); - System.out.println (list2); +}
Java Learning--collections Collection Tool class use