Introduction :The Comparable interface is used for object natural ordering. Given a collection of objects of the same type, the interface allows you to order the collection into that natural order. It has an Abstract Method compareTo().The compareTo() method compares the current instance with passed argument.
Below is an Example of Comparable . Problem :Create a program that sort Accounts by Account Number using Comparable interface.Solution:Account class will implement java.lang.Comparable interface and define compareTo method from this interface. Method compareTo will compare current object with another account object, that is passed as an argument. Comparison will be done by Account Number.Program :Step 1 - Coding(1) Account Class /* * Keeps Account information. * It Implements Comparable Interface * It has One Abstract Method Comparto which Takes Single Argument . * */ public String accountNo; public double balance; public Account() { }; public Account(String an, double bal) { accountNo = an; balance= bal; } public String getAccountNo() { return accountNo; } public void setAccountNo(String accountNo) { this.accountNo = accountNo; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } /** * Make use of one implicit object and other explicit object. * Compare Account Nos. and returns * -1 : If Account No One is less than Account No Two * 0 : If both Account Nos. are equal * 1 : If Account No One is greater than Account No Two */ } /* * A program to compare and sort Account objects using Comparable Interface * implementation. * A List contains 3 Account objects and sort it by Account Nos. in Ascending order . */ public class SortAccountListByAccountNo { public static void main(String[] args) throws IOException { ArrayList list = new ArrayList(); list.add(new Account("SBI01", 1000)); list.add(new Account("SBI01", 2000)); list.add(new Account("SBI03", 3000)); //Sort Account No. in ascending order Collections.sort(list); Iterator<Account> itr = list.iterator(); Account acc = null; System.out.println("**Account No. List( Ascending )**"); System.out.println(" "+"Account No "+" "+"Balance"); while(itr.hasNext()) { acc = itr.next(); System.out.println(" "+acc.getAccountNo() + "\t " + acc.getBalance()); } } Step 2 – Deployment
Step 3 - Testing
Output:**Account Details List( Ascending )** Account No Balance SBI01 1000.0 SBI01 2000.0 SBI03 3000.0 FAQ:Q 1. What is comparable Interface?A : The Comparable interface is used to sort collections and arrays of objects using the Collections.sort(obj) and java.utils.Arrays.sort() methods respectively. The objects of the class implementing the Comparable interface can be ordered.Comparable interface is implemented by all the wrapper classes(String, Float, Double....) and it is called to represent class's natural ordering. Q 2. When to use Comparable Interface?A : If the comparison is simpler -- such as when sorting based on only one "key" field, where the other state is irrelevant for the natural ordering -- then the code can be simplified and comparable is used. |