debugging tipsHere is our code example of how to implement Comparable interface in Java. This code is saved in a Java file called HelloComparable.java, but it contains two classes, first HelloComparable, which is a test class to demonstrate example, and second class named Bank, which is our domain object. Banks has just two fields, name and ranking. In this example, I have taken their ranking as natural order, but in some cases it may well be their name as well. Point here is whatever logic you put on compareTo() method, that becomes your natural order, so you should put the logic which is most common in your application e.g. sorting Employee objects on id is most common, and sorting Event object on date is most common. If you look at code ofcompareTo() method it just does an integer arithmetic and return result its possible because ranking are small positive number. It makes your code clean, but only if you are 100% sure that difference will not exceed maximum value of integer in product's life time, because it it happens it will create subtle bugs due to integer overflow, giving impression that your natural order sorting is working in many cases but suddenly fails. By the way, I would like to share debugging tips with such condition, if you ever face an issue, which is intermittent, focus on data and concurrency. Repeat test with same data and if you are not able to reproduce than focus on concurrency characteristic of application. Coming bank to our Comparable example, we have stored all six banks in random order in a List and using Collections.sort() method to sort them on their natural order. This is an overloaded method, where other version also expect Comparator for custom order sorting. You can also use Arrays.sort() method, if you prefer to store your object in array. Both Collections.sort() and Arrays.sort() sort objects in their natural order, defined by compareTo() method of java.lang.Comaprable interface.
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class HelloComparable { public static void main(String args[]) { Bank citibank = new Bank("Citibank", 1); Bank icici = new Bank("ICICI", 5); Bank bankOfAmerica = new Bank("BankOfAmerica", 2); Bank dbs = new Bank("DBS", 6); Bank hsbc = new Bank("HSBC", 3); Bank scb = new Bank("Standard Charted", 4); List<Bank> banks = new ArrayList<Bank>(); banks.add(citibank); banks.add(icici); banks.add(bankOfAmerica); banks.add(dbs); banks.add(hsbc); banks.add(scb); // print banks in unsorted order System.out.println("List of Banks in unsorted order" + banks); // Sort bank on their natural order, which is ranking Collections.sort(banks); // print banks in their natural order, sorted System.out.println("List of Banks in sorted order" + banks); } } class Bank implements Comparable<Bank> { private String name; private int ranking; public Bank(String name, int ranking) { this.name = name; this.ranking = ranking; } @Override public int compareTo(Bank bank) { return this.ranking - bank.ranking; // possible because ranking is small // positive integer } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ranking; return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Bank other = (Bank) obj; if (name == null) { if (other.name != null) { return false; } } else if (!name.equals(other.name)) { return false; } if (ranking != other.ranking) { return false; } return true; } @Override public String toString() { return String.format("%s: %d", name, ranking); } } Output: List of Banks in unsorted order[Citibank: 1, ICICI: 5, BankOfAmerica: 2, DBS: 6, HSBC: 3, Standard Charted: 4] List of Banks in sorted order[Citibank: 1, BankOfAmerica: 2, HSBC: 3, Standard Charted: 4, ICICI: 5, DBS: 6]
No comments:
Post a Comment