What are Java Comparators and Comparables? As both names suggest (and you may have guessed), these are used for comparing objects in Java. Using these concepts; Java objects can be sorted according to a predefined order. Two of these concepts can be explained as follows. Comparable A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances. Comparator A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.lang.Comparator interface. How to use these? There are two interfaces in Java to support these concepts, and each of these has one method to be implemented by user. Those are; java.lang.Comparable: int compareTo(Object o1) This method compares this object with o1 object. Returned int value has the following meanings. 1. positiv...
A site who helps developer to find out there solutions. We try our best to provide you the best suite solutions for your problems.Also we are ready to accept your suggestions.Please be co-operate with us for sake of your and our growing.