The Java Comparable interface is utilized for arranging objects within a user-defined class. This interface is located in the java.lang package and consists of a sole method called compareTo(Object). It offers a singular sorting sequence, meaning you can arrange elements based on a single data attribute only, such as roll number, name, age, or any other attribute.
compareTo(Object obj) method
public int compareTo(Object obj): It is used to compare the current object with the specified object. It returns
- positive integer, if the current object is greater than the specified object.
- negative integer, if the current object is less than the specified object.
- zero, if the current object is equal to the specified object.
We can sort the elements of:
- String objects
- Wrapper class objects
- User-defined class objects
Collections class
The Collections class offers static functions for arranging the items within collections. When dealing with sets or maps as collection elements, TreeSet or TreeMap can be employed for sorting. On the other hand, sorting list elements directly is not supported. To sort list type elements, the Collections class offers specific methods for this purpose.
Method of Collections class for sorting List elements
Define a method called sort that takes a List parameter. This method is intended for sorting the elements contained within the List. It is important to note that the elements within the List must implement the Comparable interface.
Note: String class and Wrapper classes implement the Comparable interface by default. So if you store the objects of string or wrapper classes in a list, set or map, it will be Comparable by default.
Java Comparable Example
Let's examine an example showcasing the Comparable interface, which facilitates sorting list elements based on their age.
File: Student.java
class Student implements Comparable<Student>{
int rollno;
String name;
int age;
Student(int rollno,String name,int age){
this.rollno=rollno;
this.name=name;
this.age=age;
}
public int compareTo(Student st){
if(age==st.age)
return 0;
else if(age>st.age)
return 1;
else
return -1;
}
}
File: TestSort1.java
Example
import java.util.*;
public class TestSort1{
public static void main(String args[]){
ArrayList<Student> al=new ArrayList<Student>();
al.add(new Student(101,"Vijay",23));
al.add(new Student(106,"Ajay",27));
al.add(new Student(105,"Jai",21));
Collections.sort(al);
for(Student st:al){
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
}
}
Output:
105 Jai 21
101 Vijay 23
106 Ajay 27
Java Comparable Example: reverse order
Let's explore another instance of the Comparable interface which arranges elements in a list based on age but in descending order.
File: Student.java
class Student implements Comparable<Student>{
int rollno;
String name;
int age;
Student(int rollno,String name,int age){
this.rollno=rollno;
this.name=name;
this.age=age;
}
public int compareTo(Student st){
if(age==st.age)
return 0;
else if(age<st.age)
return 1;
else
return -1;
}
}
File: TestSort2.java
Example
import java.util.*;
public class TestSort2{
public static void main(String args[]){
ArrayList<Student> al=new ArrayList<Student>();
al.add(new Student(101,"Vijay",23));
al.add(new Student(106,"Ajay",27));
al.add(new Student(105,"Jai",21));
Collections.sort(al);
for(Student st:al){
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
}
}
Output:
106 Ajay 27
101 Vijay 23
105 Jai 21