In Java, to check whether two objects are the same or not, we often use the equals() method to do this! To compare and sort two objects in an array or list, we can use the Comparable interface. In this tutorial, I will talk more about this interface for you to understand better!
OK, let’s get started.
Comparable interface is a generic interface that is used to sort objects that their class implements this interface.
For example, I have a Student object like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.huongdanjava.comparable; public class Student { private int code; private String name; public Student(int code, String name) { this.code = code; this.name = name; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
Now, I want to sort these Student objects in an array, the first thing I need to do is implement the Comparable interface for the Student object, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava.comparable; public class Student implements Comparable<Student> { private int code; private String name; public Student(int code, String name) { this.code = code; this.name = name; } // Getter and Setter } |
When implementing this interface, we must define the method:
1 |
public int compareTo(Object other); |
to compare this object with the object passed in the parameter of the method. The content of the method is ourself defined, but it is required to return the following values: negative, 0, and positive.
A negative number means that the object is smaller than the object passed in the method’s parameter, 0 means that the two objects are equal and the positive number means that the object is larger.
For the Student object in the above example, we can define the compareTo(Object other) method as follows:
1 2 3 4 5 6 7 8 9 |
public int compareTo(Student o) { if (this.getCode() > o.getCode()) { return 1; } if (this.getCode() < o.getCode()) { return -1; } return 0; } |
OK, about the Comparable interface, you just have to grasp the above is enough! Now, to see how it works, I’m going to use the Student object above as an example.
In this example, we will use the TreeSet object to hold several Student objects. Because, the TreeSet object contains elements in sorted order, so when you add new Student objects, it will automatically rearrange them.
Code as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.huongdanjava.comparable; import java.util.Iterator; import java.util.TreeSet; public class TestComparable { public static void main(String args[]) { TreeSet<Student> students = new TreeSet<Student>(); students.add(new Student(21, "Khanh")); students.add(new Student(26, "Tan")); students.add(new Student(24, "Phuong")); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next().getCode()); } } } |
Result: