Java Learning--collections Collection Tool class use

Source: Internet
Author: User

* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.