Comparable Interface
- Eclipse: Oxygen
- Java: 1.8
Comparable Interface
The comparable class is shown here. The syntax of the comparable class is unique to the others which include objects and the class. This class can be used for various purposes as shown in the tutorial. The Comparable interface is found in java.lang package. It contains only one method named compareTo(Object).
A comparable Interface is used to sort the custom-type objects. By default, a class defined by the user is not comparable. Hence, their objects cannot be compared. To make an object comparable, the class must implement a comparable interface.
A comparable interface can be used to achieve a single way of sorting like ascending or descending sorting the numerical collection or alphabetical sorting for string object collection.
In the following program, class data implements a Comparable interface. Implement the unimplemented compareTo() method based on the length of the String variable. This method accepts an object of the same instance type and based on certain criteria defined, it returns a negative value if the current value is less than the value we are comparing with, respectively.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
class Names implements Comparable<Names> { private String name; public Names(String name) { this.name = name; } @Override public int compareTo(Names obj) { if (name.length() == obj.name.length()) { return 0; } else if (name.length() < obj.name.length()) { return -1; } else { return 1; } } @Override public String toString() { return name; } } public class App { public static void main(String[] args) { List names = new LinkedList<>(); names.add(new Names("Chaand")); names.add(new Names("Ed")); names.add(new Names("John")); names.add(new Names("Mia")); App app = new App(); app.printList(names); Collections.sort(names); System.out.println("******************"); app.printList(names); } void printList(List<Names> list) { ListIterator<Names> iterator = list.listIterator(); while (iterator.hasNext()) { System.out.println("Element: " + iterator.next()); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Element: Chaand Element: Ed Element: John Element: Mia ****************** Element: Ed Element: Mia Element: John Element: Chaand |
Let’s see the same example of the Comparable interface that sorts the list elements on the basis of name length in reverse order(modify the implementation of compareTo() method).
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
class Names implements Comparable<Names> { private String name; public Names(String name) { this.name = name; } @Override public int compareTo(Names obj) { if (name.length() == obj.name.length()) { return 0; } else if (name.length() < obj.name.length()) { return 1; } else { return -1; } } @Override public String toString() { return name; } } public class App { public static void main(String[] args) { List names = new LinkedList<>(); names.add(new Names("Chaand")); names.add(new Names("Ed")); names.add(new Names("John")); names.add(new Names("Mia")); App app = new App(); app.printList(names); Collections.sort(names); System.out.println("******************"); app.printList(names); } void printList(List<Names> list) { ListIterator<Names> iterator = list.listIterator(); while (iterator.hasNext()) { System.out.println("Element: " + iterator.next()); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Element: Chaand Element: Ed Element: John Element: Mia ****************** Element: Chaand Element: John Element: Mia Element: Ed |
Contributed by: Poonam Tomar