1.

Differentiate between Comparable and Comparator in the context of Java.

Answer»
COMPARABLEComparator
A single sorting sequence is provided by Comparable. To put it another way, we can sort the collection by a single attribute such as id, NAME, or price.Multiple sorting sequences are available in the Comparator. To put it another way, we can sort the collection based on different criteria such as id, name, and price.
To sort elements, Comparable provides the compareTo() method.To order elements, the Comparator provides the compare() method.
It is PRESENT in the java.lang package.It is present in the java.util package.
The original class is affected by Comparable, i.e. the real class is changed. The original class is UNAFFECTED by the comparator, i.e. the real class is unaffected.
The Collections.sort(LIST) method can be used to sort Comparable type list members.The Collections.sort(List, Comparator) method can be used to sort the list components of the Comparator type.


Discussion

No Comment Found