In this article, we will discuss Comparable interface with its only important method compareTo() in detail
String & wrapper classes’ implements comparable interface and provide default natural sorting-order
Comparable interface:
- Comparable interface is used to order objects of each class that implements it
- This is part of original collection framework introduced in Java 1.2 version
- Present in java.lang package
- Fully qualified name is java.lang.Comparable
- Contains only one method compareTo() which returns integer value
- Method signature:
public int compareTo(Object obj);
- String & wrapper classes’ like Integer, Double, etc implements Comparable interface and provides default natural sorting-order [DNSO]
- We can reverse natural sorting-order by overriding compareTo() method and coding our own logic
- Based on the integer value returned by compareTo(object2) method, elements are stored/sorted,
1. returns negative integer, if object1 less than object2 (object1 < object2)
2. returns positive integer, if object1 greater than object2 (object1 > object2)
3. returns Zero (0), if object1 is equal to object2 (object1 == object2) - Collection classes like TreeSet & TreeMap uses compareTo() method to sort elements before storing
Example on Comparable interface:
Customer.java
- Customer POJO with 2 member variables of Integer and String type
- which implements Comparable interface
- to provide natural ordering of Customer objects on the basis of customer name
package in.bench.resources.java.collection; public class Customer implements Comparable<Customer> { // member variables int customerId; String customerName; // 2-arg parameterized constructor public Customer(int customerId, String customerName) { super(); this.customerId = customerId; this.customerName = customerName; } // override toString() method @Override public String toString() { return "Customer [" + "customerId=" + customerId + ", customerName=" + customerName + "]"; } // override compareTo() method @Override public int compareTo(Customer o) { return this.customerName.compareTo(o.customerName); } }
Main class
- This class uses above customer POJO to store objects inside TreeSet
- prints customer objects in ascending sorting-order of customer name
CustomerNatrualOrder.java
package in.bench.resources.java.collection; import java.util.TreeSet; public class CustomerNatrualOrder { // main() method public static void main(String[] args) { // creating TreeSet object of type String TreeSet<Customer> ts = new TreeSet<Customer>(); // adding elements to TreeSet object ts.add(new Customer(101, "Sundar Pichai")); ts.add(new Customer(107, "Satya Nadella")); ts.add(new Customer(103, "Shiv Nadar")); ts.add(new Customer(102, "Shantanu Narayen")); ts.add(new Customer(104, "Francisco D’Souza")); ts.add(new Customer(106, "Vishal Sikka")); ts.add(new Customer(105, "Chanda Kochhar")); System.out.println("Natural ordering of Customer Name\n"); // natural ordering of customer name for(Customer cust : ts){ System.out.println(cust.customerId + " " + cust.customerName); } } }
Output:
Natural ordering of Customer Name 105 Chanda Kochhar 104 Francisco D’Souza 107 Satya Nadella 102 Shantanu Narayen 103 Shiv Nadar 101 Sundar Pichai 106 Vishal Sikka
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html
Happy Coding !!
Happy Learning !!