Collectionsdemo1+2 List Collection general elements of sorting, custom types of sorting

Source: Internet
Author: User

CollectionsDemo1 List Collection general elements of the sort, natural sort

/*** Sort List Collection sorted by list collection using the collection's tool class collections static method Sort, * This method can be used to sort the specified list set naturally (small to large)*/ Public classSortDemo1 { Public Static voidMain (string[] args) {//1, generate a List<insteger> collectionlist<integer> list =NewArraylist<integer>(); //2. Add a random 10 random integer to the collection as an elementRandom Rand =NewRandom ();  for(inti=0;i<10;i++) {List.add (Rand.nextint (100)); }        //3. Output collection, view elements in listSYSTEM.OUT.PRINTLN (list);//[ +, +, +,--------]//4. Sort the collection by using one of the static methods of the collections classCollections.sort (list); //5. Output collection, view elements in listSYSTEM.OUT.PRINTLN (list);//[ A, A, a, V, V, +, +, +, +]//6. Test for summary 2, sort the stringList<string> strlist =NewArraylist<string>(); Strlist.add ("Mary"); Strlist.add ("Lee"); Strlist.add ("Varen"); Strlist.add ("Garen"); Strlist.add (Dad); Strlist.add ("Wayne"); Strlist.add ("Ah-full"); Strlist.add ("Genghis Khan in Beijing"); Strlist.add (Genghis Khan); //7. Output ResultsSystem.out.println (strlist);        Collections.sort (strlist);        System.out.println (strlist); /** [Mary, Lee, Varen, Garen, Dad, Wayne, AH-full, Genghis Khan in Beijing, Genghis Khan] [Garen, Lee, Mary, Varen, Genghis Khan, Genghis Khan in Beijing, Dad, Wayne, AH-full]*/        //8. For summary 3, test whether custom elements can be sorted using the secondary methodList<point> pointlist =NewArraylist<point>(); Pointlist.add (NewPoint (3,7)); Pointlist.add (NewPoint (3,5)); Pointlist.add (NewPoint (5,2)); Pointlist.add (NewPoint (4,3));//Collections.sort (pointlist); Compilation Error    }    /*** Summary * 1. Can only be sorted from small to large according to the natural order of the Collections.sort method.     * 2. What sort of order do you want to sort the values in?     * Test Conclusion: Strings can also be sorted, according to the ASCII value of the English alphabet, from small to large. Chinese is irregular.     * 3. Can you sort on a custom type element?     * Test Conclusion: Collections.sort (pointlist); Compile error. * Note that using this method alone does not sort the custom type elements, but requires another workaround*/}

CollectionsDemo2 Ordering of List custom type elements

/*** Ordering of custom type elements, using collections static method sort How to do it? * Note: Here is the first custom class (point) to experiment with the following demo * Key: Sort elements in a collection using the comparator interface     * Summary: 1. The method of sorting the custom elements can be implemented by Collections.sort.     The premise is that you need to implement the comparator interface and implement the CompareTo method to define the comparison rules.     2. However, one drawback is that in order to implement the collections.sort approach, it is necessary to define other methods in our own classes, "intrusive" is too strong, there is no better solution? */ Public classCollectionsDemo2 { Public Static voidMain (string[] args) {//1. Create a collection of custom elements listlist<point> list =NewArraylist<point>(); //2. Add an object of type point in the collectionList.add (NewPoint (8,3)); List.add (NewPoint (10,9)); List.add (NewPoint (5,6)); List.add (NewPoint (2,3)); List.add (NewPoint (1,5));    SYSTEM.OUT.PRINTLN (list); //Output unsorted Results//[Point [X=8, Y=3], point [x=10, y=9] , point [X=5, Y=6], point [x=2, Y=3], point [X=1, Y=5]]//3. We know that sorting with Collections.sort (list) will cause an error        /*key point: Use the comparator interface to sort the elements in the collection first the point class needs to implement the comparator interface and implement its CompareTo abstract method.         It is important to note that the Compartor<t> interface can also specify generics. Generally defined as a type of similarity. CompareTo (Specified object) method returns the value of the Int return value >0 This object is greater than the specified object return value <0 This object is less than the specified object return value =0 This object equals the specified object*/Collections.sort (list); //after the point class overrides the CompareTo method, Collections.sort compiles the//[Point [X=2, Y=3], point [X=1, y=5] , point [X=5, Y=6], point [X=8, Y=3], point [x=10, Y=9]]SYSTEM.OUT.PRINTLN (list);//output sorted after result    }}

Point class for experiments

 Public classPointImplementsComparable<point>{    Private intx; Private inty;  PublicPoint (intXinty) {Super();  This. x =x;  This. y =y; }     Public intGetX () {returnx; }     Public voidSetX (intx) { This. x =x; }     Public intGetY () {returny; }     Public voidSety (inty) { This. y =y; } @Override PublicString toString () {return"Point [x=" + x + ", y=" + y + "]"; } @Override Public intcompareTo (Point O) {/** The size of the coordinate point, which is more than the distance from the point to the distance, that is, x*x+y*y than the two coordinate points **/        intTleng = This. x* This. x + This. y* This. Y; intOleng = o.x*o.x + o.y*o.y; returnTleng-Oleng; }}
View Code

Collectionsdemo1+2 List Collection general elements of sorting, custom types of sorting

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.