Java Program Using Comparable Interface
Friends, In our previous post we have seen java.util.Comparator tutorial, Here we are going to show you the implementation example of java.lang.Comparable interface. The Comparable interface is one of the very popular interface in java. It has a single public method public int compareTo(T o) If you are planing to use your object in Collection and may needs to sort the elements of that Collection than you must have to implement java.lang.Comparable or java.util.Comparator interface, here we are going to implement Comparable interface. Although we have listed few key differences between java.lang.Comparable and java.util.Comparator interface . The sorting of element is only possible after the comparison of the elements, that is why Comparable interface has a compareTo(T o) method. The implemented class must have to provide the implementation of compareTo(T o) method, the compareTo method is almost same as equals(Object o) method of java.lang.Object class except that compareTo meth...