Object sorting in Java Collection
The object sorting requirements in the set are still quite common. Of course, we can rewrite the equals method and compare them cyclically. At the same time, Java provides us with an APIs that is easier to use. When the set or array to be sorted is not simply numeric, Comparator or Comparable can be used to sort objects or customize the sorting in a simple way.
In the following two examples, Comparable and Comparator are used to sort the age of User objects.
The Comparable interface method class implements the compareTo method in the Comparable interface.
Import java. util. arrays; 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 age) {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 ComparableUser [] {new ComparableUser ("u1001", 25), new ComparableUser ("u1002", 20), new ComparableUser ("u1003 ", 21)}; Arrays. sort (users); for (int I = 0; I <users. length; I ++) {ComparableUser user = users [I]; System. out. println (user. getId () + "" + user. getAge ());}}}
The Comparator interface is used to write a new class, implement the Comparator interface, and implement the compare method in the interface.
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 age) {this.age = age;}public String getId() {return id;}public void setId(String id) {this.id = id;}}
Import java. util. arrays; import java. util. comparator; 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", 25), new User ("u1002", 20), new User ("u1003 ", 21)}; Arrays. sort (users, new UserComparator (); for (int I = 0; I <users. length; I ++) {User user = users [I]; System. out. println (user. getId () + "" + user. getAge ());}}}
Summary
So which method should we choose, the Comparable interface or the Comparator?
If a class implements the Comparable interface, it indicates that the objects of this class can be compared with each other. A set composed of these class objects can be directly sorted using the sort method.
Comparator can be seen as an algorithm implementation that separates algorithms from data. Comparator can also be used in the following two environments:
Comparable is not implemented because the comparison problem is not taken into account during design. You can use Comparator to sort objects without changing the object itself. You can use multiple sorting criteria, such as ascending or descending.