Sorting in Custom TreeSet

Source: Internet
Author: User

The first example of code:

Public class user implements comparable<user> {    private  String name;    private int age;    private  Int ordernumber;    public user (String name, int age, int  ordernumber)  {        this.name = name;         this.age = age;         this.orderNumber = orderNumber;    }    public  String getname ()  {        return name;     }    public void setname (String name)  {         this.name = name;    }     Public int getage () &NBsp {        return age;    }     public void setage (int age)  {         This.age = age;    }    public int getordernumber ( )  {        return ordernumber;    }     public void setordernumber (Int ordernumber)  {         this.orderNumber = orderNumber;    }      @Override     public int compareto (user other)  {         if  (This.getordernumber ()  == other.getordernumber ())  {            return this.getage ()   - other.getage ();         }        return  This.getordernumber ()  - other.getordernumber ();     }    @ Override    public string tostring ()  {         return  "User [name="  + name +  " age="  + age +   ",  ordernumber="  + orderNumber +  "]";     }}

The second example of code:

public class student {    private string name;     private int score;    public student (String name, int  Score)  {        super ();         this.name = name;        this.score =  score;    }     @Override     public string  tostring ()  {        return  "Student [name="   + name +  ",  score="  + score +  "]";    }     public string getname ()  {         Return name;    }    public void setname (String  Name)  {        this.name = name;    }     Public int getscore ()  {        return score;     }    public void setscore (Int score)  {         this.score = score;    }}public class  StudentComparator implements Comparator<Student>{     @Override      public int compare (STUDENT&NBSP;S1,&NBSP;STUDENT&NBSP;S2)  {         return s2.getscore ()  - s1.getscore ();     }}

Test the code (the first and second are written together):

Public class testtreeset {    public static void main (String [] args)  {        TreeSet<User> set =  New treeset<> ();         user user1 = new  user ("Tom",  25, 3);         user user2 =  new user ("Jack",  28, 2);         user user3  = new user ("Mike",  20, 3);        user  User4 = new user ("John",  30, 1);         user  user5 = new user ("Jill",  50, 0);                 set.add (user1);         set.add (User2);        set.add (User3);         Set.add (USER4);         set.add (USER5);      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (SET);                          Set<Student> studens = new TreeSet<> (new  Studentcomparator ());        student s1 = new  Student ("Tom",  80);        student s2 = new  Student ("Jack",  95);        student s3 = new  Student ("Mike",  60);        student s4 = new  Student ("John",  75);   &nbSp;     student s5 = new student ("Jill",  100);                 studens.add (S1);         studens.add (S2);         Studens.add (S3);         studens.add (S4);         studens.add (S5);                 system.out.println (studens);     }}


Sorting in Custom TreeSet

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.