Comparable is an interface in Java and it comes from java.lang package. This interface is used to compare objects in a class and sorts them according to the natural/reversed order. Hence by using this interface we can sort an array of custom objects.
Lists and arrays of objects that implement Comparable interface can be sorted automatically by Collections.sort() and Arrays.sort() methods. The object will be arranged depending upon the compareTo() method available in the implemented class. Let us discuss the Comparable interface in detail with examples in this tutorial.
Using Comparable interface we can sort :
- String objects
- Wrapper class objects
- User-defined class objects
Methods in Comparable Interface:
public int compareTo(Object o);
This method compares the object for the specified object for order. It returns the below values when comparing the object corresponding to 3 cases as:
- Firstly, if the first element is less than the second element it returns “-1″.
- In the case that the first element equals to the second element, it returns “0”.
- The last case in which the first element is greater than the second element, it returns “1”.
Comparable Example
For example, we are going to sort the Students’ age in the Student object using the comparable interface and compareTo() method.
class Student implements Comparable<Student>{
int id;
String name;
String regNumber;
int age;
String gender;
public Student(int id, String name, String regNumber, int age, String gender) {
this.id = id;
this.name = name;
this.regNumber = regNumber;
this.age = age;
this.gender = gender;
}
@Override public int compareTo(Student student) {
if (age == student.age) {
return 0;
} else if (age > student.age) {
return 1;
} else {
return -1;
}
}
}
Creating a program ComparableDemo.java for accessing the student class.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ComparableDemo {
public static void main(String[] args) {
List <Student> list = new ArrayList <Student>();
list.add(new Student(1, "ChandraShekhar", "2015011501", 24, "M"));
list.add(new Student(2, "John", "5623521", 35, "M"));
list.add(new Student(3, "Smith", "986623", 30, "M"));
list.add(new Student(4, "Joi", "856421", 23, "F"));
Collections.sort(list);
int size = list.size();
for (int i = 0; i < size; i++) {
Student student = list.get(i);
System.out.print(student.age + " \n");
}
}
}
Output
23
24
30
35
Comparable Example in Reverse order:
Let us see the same example shown above with a small change. As a result, sorting will be done on the age in reverse order.
class Student implements Comparable<Student>{
int id;
String name;
String regNumber;
int age;
String gender;
public Student(int id, String name, String regNumber, int age, String gender) {
this.id = id;
this.name = name;
this.regNumber = regNumber;
this.age = age;
this.gender = gender;
}
@Override public int compareTo(Student student) {
if (age == student.age) {
return 0;
} else if (age < student.age) {
return 1;
} else {
return -1;
}
}
}
In the above program ‘Comparable2.java’, we did a small correction compared to the ‘Comaparable.java’. That is we changed the condition from age>student.age
to age<student.age
which returns 1 if the first element is less than the second element. Therefore we get the students’ age in the reverse order. Now, we create a program ‘ComparableDemo2.java’ same as the ‘Comparable.java’ without any change for accessing the student class.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ComparableDemo {
public static void main(String[] args) {
List <Student> list = new ArrayList <Student>();
list.add(new Student(1, "ChandraShekhar", "2015011501", 24, "M"));
list.add(new Student(2, "John", "5623521", 35, "M"));
list.add(new Student(3, "Smith", "986623", 30, "M"));
list.add(new Student(4, "Joi", "856421", 23, "F"));
Collections.sort(list);
int size = list.size();
for (int i = 0; i < size; i++) {
Student student = list.get(i);
System.out.print(student.age + " \n");
}
}
}
Output
35
30
24
23
Hence, we can see that the age given in the list is sorted in reverse order. To sum up, for comparing single bean properties we can use the Comparable interface and override the method compareTo(Object o).
Happy Learning 🙂