Comparable and comparator implementation object comparisons in Java

Source: Internet
Author: User
Tags comparable

When a collection or array that needs to be sorted is not a purely numeric type, you can usually use comparator or comparable to implement object sorting or custom sorting in a simple way. The following two examples are used to sort the age of the user object using comparable and comparator respectively.

1. By implementing the comparable interface, sort according to the age of the user.

Import Java.util.Arrays;       /** * @author PENGCQU * * */public class Comparableuser implements comparable {private String ID;         private int age;           Public Comparableuser (String ID, int age) {this.id = ID;       This.age = age;       } public int Getage () {return age;       public void Setage (int.) {this.age = age;       } public String GetId () {return id;       } public void SetId (String id) {this.id = ID;       } public int compareTo (Object o) {return this.age-((Comparableuser) O). Getage (); }/** * Test method * * public static void Main (string[] args) {comparableuser[] users = new C                   Omparableuser[] {new Comparableuser ("u1001"), New Comparableuser ("u1002", 20),           New Comparableuser ("u1003", 21)};           Arrays.sort (users); for (int i = 0; I &Lt Users.length;               i++) {Comparableuser user = Users[i];           System.out.println (User.getid () + "" + user.getage ());   }       }     }

2. By implementing the comparator interface, sort according to the age of the user.

public class User {         private String ID;       private int age;         Public User (String ID, int age) {           this.id = ID;           This.age = age;       }         public int getage () {           return age;       }         public void Setage (int.) {           this.age = age;       }         Public String getId () {           return ID;       }         public void SetId (String id) {           this.id = ID;       }            }  

Import Java.util.Arrays;   Import Java.util.Comparator;     /**   * @author pengcqu   *    *  /public class Usercomparator implements Comparator {public         int compare ( Object arg0, Object arg1) {           return (user) arg0). Getage ()-((user) arg1). Getage ();         /**       * Test method * * Public      static void Main (string[] args) {           user[] users = new user[] {new User ("u1001", 2 5),                   New User ("u1002"), New User ("u1003")};           Arrays.sort (Users, New Usercomparator ());           for (int i = 0; i < users.length; i++) {               User user = Users[i];               System.out.println (User.getid () + "" + user.getage ());}}}     

Select comparable interface or comparator?

A class that implements the comparable interface indicates that objects of this class can be compared to each other, and that a collection of such objects can be sorted directly using the Sort method.

Comparator can be regarded as an algorithm implementation, the algorithm and data separation, comparator can also be used in the following two kinds of 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, can use a variety of sorting standards, such as ascending, descending and so on.


Comparable and comparator implementation object comparisons in Java

Related Article

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.