

Note: It is generally a good idea for comparators to also implement This is contrary to the specification of the True (and the size of the tree set will increase) because a andī are not equivalent from the tree set's perspective, even though


Map), which is defined in terms of equals.įor example, suppose one adds two elements a and b such that Sorted set (or sorted map) will violate the general contract for set (or The sorted set (or sorted map) will behave "strangely." In particular the Ordering imposed by c on S is inconsistent with equals, Is used with elements (or keys) drawn from a set S. Suppose a sorted set (or sorted map) with an explicit comparator c Ordering inconsistent with equals to order a sorted set (or sorted map).

S is said to be consistent with equals if and only ifĬ.compare(e1, e2)=0 has the same boolean value asĬaution should be exercised when using a comparator capable of imposing an The ordering imposed by a comparator c on a set of elements Objects that don't have a natural ordering. Comparators can also be used to control the order ofĬertain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of Comparators can be passed to a sort method (suchĪs Collections.sort or Arrays.sort) to allow precise control positive integer value, if the current object is greater than the specified object (this > object).A comparison function, which imposes a total ordering on someĬollection of objects.It returns an indication that is as follows: The compareTo() method is used to compare the current object with the specified object. This method is defined in Comparable interface for comparing objects (defined in java.lang package) like this: The signature of this method is as follows: public int compareTo(Object obj) The Comparable interface provides only one abstract method that is used to determine the natural ordering of instances of a class. Several classes in Java library such as BigDecimal, BigInteger, Boolean, Byte, ByteBuffer, Calendar, String, etc implement Comparable interface in java to define a natural order for objects. Here, T represents the type of objects that has to be compared. Comparable Interface declarationĬomparable in Java is a generic interface that can be declared like this in the general form: public interface Comparable Look at the below realtime example to understand better. The purpose of using Comparable interface and its compareTo() method is to compare one object with another object in a way less than (). To accomplish this purpose, Java provides Comparable interface. In order to compare it, the two objects must be comparable. Suppose we want to find the larger or smaller of two objects of the same type, such as two students, two employees, two dates, two rectangles, two squares, or two circles. When to use Comparable interface in Java? It is present in package and contains only one method named compareTo(Object). So, if you want to store objects in a sorted collection, its class must implement Comparable interface.Ĭomparable interface was introduced in Java 1.0 version. The sorted collections use the natural sorting order defined by a class to determine the order of objects. Java Comparable interface can be implemented by any custom class or user-defined class if you want to use Arrays or Collections sorting methods. For numeric values, it is numeric order (i.e. Natural ordering means usual ordering.įor example, the natural order of strings is alphabetical (i.e. It is mainly used to provide the natural sorting order of objects (elements). In other words, Comparable interface defines a standard way in which two objects of the same class will be compared.
