Collections classes that specialize in manipulating collections

Source: Internet
Author: User
Tags shuffle

Sort Sort

BinarySearch Find

Fill Change all of the list

ReplaceAll covers all the same

Reverse Invert the contents of a collection

Swap swap Operations

Shuffle random allocation, is the order of disruption

Comparator use of external comparators

Import java.util.*;p ublic class anli{public static void Main (string[] args) {//binarysearchdemo ();//reverseorderdemo () ; Randomdemo ();} public static void Randomdemo () {Random rd = new random ();//random integer, Random is non-generic, without parameter sop (Rd.nextint (10));//Specified range}public static void Binarysearchdemo () {list<string> List = new arraylist<string> () List.add ("ADF"); List.add (" Adgaf "), List.add (" ADFRWREWRDF "), List.add (" ADSFASFF "), List.add (" AF "), List.add (" ARWDFC "), SOP (list); Collections.sort (list,new com ()),//collections.sort (list), SOP (list), int index = Collections.binarysearch (list, "AF" , new COM ());//To pass the comparator over to SOP (index);//collections.fill (list, "AV"); Change the list of all//sop (lists),//collections.replaceall (list, "av", "va");//cover all the same//sop (list); Collections.reverse (list); SOP (list); Collections.swap (list,0,1);//exchange operation, must be in the list collection. SOP (list); Collections.shuffle (list);//Randomly assigned SOP (list);} public static void Sop (Object obj) {System.out.println (obj);} public static void Reverseorderdemo () {//treeset<string> ts = new treeset<String> (Collections.reverseorder ());//Because it is a return comparator, it needs to be set or map structure//treeset<string> ts = new treeset< String> (Collections.reverseorder (New COM ()));//The order in which the comparator of the strong turn changes. treeset<string> ts = new treeset<string> (new COM ()), Ts.add ("ADF"), Ts.add ("ASFADF"), Ts.add ("Agsfaddf"); Ts.add ("ADSDDSF"); Ts.add ("asdf"); Ts.add ("AF");iterator<string> it = Ts.iterator (); while (It.hasnext ()) { String str = it.next (); SOP (str);}}} Class COM implements comparator<string>{public int compare (String one,string) {int value = One.length ()- Two.length (); if (One.length () ==two.length ()) {return One.compareto (both);} return value;}}



Collections classes that specialize in manipulating collections

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.