Java Learning (16): Custom Comparisons of objects, comparator and comparable

Source: Internet
Author: User

Implement custom sorting of objects with two examples

1, realize comparator interface.

1 Importjava.util.ArrayList;2 Importjava.util.Collections;3 ImportJava.util.Comparator;4 Importjava.util.List;5 6  Public classStudentcomparatorimplements Comparator<object>7 {8 @Override9     public int Compare (object arg0, Object arg1)Ten     { One         return ((Student) arg0). GetId ()-((Student) arg1). GetId (); A     } -      -      Public Static voidMain (string[] args) the     { -Student STU1 =NewStudent (One, "A"); -Student STU2 =NewStudent ("B")); -Student STU3 =NewStudent ("C"); +Student Stu4 =NewStudent ("D"); -List<student> stulist =NewArraylist<student> (4); + Stulist.add (STU1); A Stulist.add (STU2); at Stulist.add (STU3); - Stulist.add (STU4); -         Collections.sort (stulist, New Studentcomparator ()); - System.out.println (stulist); -     } - } in  - classStudent to { +     Private intID; -      the     PrivateString name; *      $      PublicStudent (intID, String name)Panax Notoginseng     { -          This. ID =ID; the          This. Name =name; +     } A      the      Public intgetId () +     { -         returnID; $     } $      -      Public voidSetId (intID) -     { the          This. ID =ID; -     }Wuyi      the      PublicString getName () -     { Wu         returnname; -     } About      $      Public voidsetName (String name) -     { -          This. Name =name; -     } A      +      PublicString toString () the     { -         return  This. GetId () + This. GetName (); $     } the}

Output: [11A, 22C, 33B, 55D]

2. Implement the comparable interface directly on the classes that need to be sorted

1 Importjava.util.ArrayList;2 Importjava.util.Collections;3 Importjava.util.List;4 5  Public classStudentimplements Comparable<object>6 {7     Private intID;8     9     PrivateString name;Ten      One      PublicStudent (intID, String name) A     { -          This. ID =ID; -          This. Name =name; the     } -      -      Public intgetId () -     { +         returnID; -     } +      A      Public voidSetId (intID) at     { -          This. ID =ID; -     } -      -      PublicString getName () -     { in         returnname; -     } to      +      Public voidsetName (String name) -     { the          This. Name =name; *     } $     Panax Notoginseng      PublicString toString () -     { the         return  This. GetId () + This. GetName (); +     } A      the @Override +     public int CompareTo (Object o) -     { $         return This.getid ()-((Student) O). GetId (); $     } -      -      Public Static voidMain (string[] args) the     { -Student STU1 =NewStudent (One, "A");WuyiStudent STU2 =NewStudent ("B")); theStudent STU3 =NewStudent ("C"); -Student Stu4 =NewStudent ("D"); WuList<student> stulist =NewArraylist<student> (4); - Stulist.add (STU1); About Stulist.add (STU2); $ Stulist.add (STU3); - Stulist.add (STU4); - Collections.sort (stulist); - System.out.println (stulist); A     } +      the}

Same output: [11A, 22C, 33B, 55D]

3, a class implementation of the comparable interface indicates that the class itself can be compared between the object, the collection of such objects can be directly used by the sort method.

Comparator can be regarded as an implementation of an algorithm, separating the algorithm from the data. Comparator can also be used in the following two environments:

(1) The designer of the class did not consider the comparison problem and did not implement comparable, can be comparator to achieve the sort without having to change the object itself.

(2) You can use a variety of sorting criteria, such as the definition of ascending class, descending class, etc.

Java Learning (16): Custom Comparisons of objects, comparator and comparable

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.