Collection class and common APIs, collectionapi

Source: Internet
Author: User

Collection class and common APIs, collectionapi

Collection class and common APIs

Collection-class set tool class, which defines several algorithms used for class set and ing. These algorithms are defined as static methods. For details, refer to the api documentation;

A) public static void sort (List <T> liet)

B) public static int binarySearch (List <T> list, T key)

C) public static void reverse (List <?> List)

D) public static void shuffle (List <?> List)

E) public static void swap (List <?> List, int I, int j)

F) public static <T> void fill (List <? Super T> list, T obj)

1 List <String> nList = new ArrayList <String> (); 2 nList. add ("zhangsan"); 3 nList. add ("lisi"); 4 nList. add ("wangwu"); 5 nList. add ("zhaoliu"); 6 nList. add ("tianqi"); 7 System. out. println ("before"); 8 for (String s: nList) {9 System. out. print (s + ""); 10} 11 12 System. out. println (); 13 System. out. println ("after switching order"); 14 Collections. swap (nList, 1, 2); 15 for (String s: nList) {16 System. out. print (s + ""); 17} 18 19 System. out. println (); 20 System. out. println ("after natural sorting"); 21 Collections. sort (nList); 22 for (String s: nList) {23 System. out. print (s + ""); 24} 25 26 System. out. println (); 27 System. out. println ("binary lookup"); 28 System. out. println (Collections. binarySearch (nList, "zhaoliu"); 29 30 System. out. println ("disordered order"); 31 Collections. shuffle (nList); 32 for (String s: nList) {33 System. out. print (s + ""); 34} 35 36 System. out. println (); 37 System. out. println ("fill"); 38 Collections. fill (nList, "jay"); 39 for (String s: nList) {40 System. out. print (s + ""); 41}

Output result:

Before operation

Zhangsan lisi wangwu zhaoliu tianqi

After switching order

Zhangsan wangwu lisi zhaoliu tianqi

After natural sorting

Lisi tianqi wangwu zhangsan zhaoliu

Binary Search

4

Disordered order

Zhaoliu tianqi wangwu zhangsan lisi

Fill

Jay

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.