The Java API library provides interfaces solely to establish a system of ordering on the objects of a class. They can be imposed implicitly as its natural principle of ordering or explicitly appended to customize the order as per requirement. This principle of ordering relies on the meaningful comparison method as implemented by the classes. These interfaces are basically comparators: one provided in the java.lang library, and another in the java.util package called Comparable and Comparator , respectively.
This interface is a part of java.lang package. Many classes in the Java API library implement this interface, such as Byte, Character, Double, Float, Long, Short, String, Integer , and so forth. This enables a list of objects of these classes to be sorted automatically by the Collection.sort function. Implementation of this interface enables the objects of this class to be used as keys in a sorted map or elements in a sorted set without using any explicit comparators. Any class that wants to impose ordering on the objects implements this interface. It is a generic interface; and the most important method declared by it is:
This method determines the natural ordering of instances of a class. For example, the natural ordering of a String , Integer , or a Date is deemed to be in ascending order.
Output:
Observe how elements are automatically ordered alphabetically as per the natural ordering principle, from lowest to highest. We have not used the Comparable interface explicitly in the code.