Comparable and Comparator Interfaces in Java
As their name suggest, Comparable and Comparator Interfaces are used to compare similar objects for example, while performing searching and sorting.
Assume that you have a container containing a list of Person object.Now, how you compare two Person objects? There are any number ofcomparable attributes, such as SSN, name, driving-license number, and so on. Two objects can be compared on SSN as well as person’s name; this depends on the context. Hence, the criterion to compare the Person objects cannot be predefined; a developer has to define this criterion. Javadefines Comparable and Comparator interfaces to achieve the same.