TreeSet sorting a custom object

Source: Internet
Author: User

 PackageCn.itcast.day21.treeset2;ImportJava.util.Comparator;ImportJava.util.TreeSet;/** TreeSet Sort by the name length comparer of the custom object student * * Natural sort or comparator sort depending on, TreeSet construction method * Parameterless Construction: Natural sort * with parametric construction: comparator sort * * TreeSet The principle of the collection guarantees the ordering and uniqueness of the elements * uniqueness: Based on whether the return of the comparison is determined by the * Order: * A: Natural sort (the element is comparative) * Let the class that the element belongs to implements the natural sort interface comparable * B: ratio Sort by (collection is comparative) * Let the constructor of the collection receive a subclass object of the Comparer interface comparator **/ Public classTreesetdemo { Public Static voidMain (string[] args) {//To create a collection object//treeset<student> ts=new treeset<student> ();//Natural Sort//treeset<student> ts= TreeSet (Comparator Comparator);//Comparator sorting---recommended! Because anonymous inner classes have less impact on other codeTreeset<student> ts=NewTreeset<student> (NewComparator<student>() {@Override Public intCompare (Student O1, Student O2) {intNum=o1.getname (). Length ()-o2.getname (). Length (); intNum2=num==0?o1.getname (). CompareTo (O2.getname ()): num; intNum3=num2==0?o1.getage ()-o2.getage (): num2; returnnum3;        }        }); //Creating an Element objectStudent s1=NewStudent ("Linqingxia", 27); Student S2=NewStudent ("Wuqilong", 27); Student S3=NewStudent ("Wanglihong", 34); Student S4=NewStudent ("Zhouxingchi", 57); Student S5=NewStudent ("Linqingxia", 28); Student S6=NewStudent ("Linqingxia", 27); //adding a collection elementTs.add (S1);        Ts.add (S2);        Ts.add (S3);        Ts.add (S4);        Ts.add (S5);                Ts.add (S6); //iterating through the collection         for(Student s:ts) {System.out.println (S.getname ()+"-----"+s.getage ()); }    }}
 PackageCn.itcast.day21.treeset2; Public classStudent {PrivateString name; Private intAge ;  PublicStudent (String name,intAge ) {        Super();  This. Name =name;  This. Age =Age ; }     PublicStudent () {Super(); }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }}

Natural sort and comparator sort which is better?

Recommendation: If you use only one time, it is recommended that you implement the comparer sort by using an anonymous inner class. (Constructs for collections)

The benefit of this: when the student class changes, you do not have to modify the overall comparison logic, that is, the CompareTo () method (which has the comparative nature of the elements) without modifying the natural sort dependency

TreeSet sorting a custom object

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.