"Comparable and comparator source"
java.lang.Comparable Public Interface Comparable<t> { publicint comparato (T o);} ------------------------------------------------------------------java.util.Comparatorpublic Interface comparator<t> { int compare (T o1,t O2); Boolean equals (Object obj);}
"Comparable Interface"
If a class implements the comparable interface, it means that the class supports sorting. Assuming there is a list (or array) of objects for the class that implements the comparable interface, the list (or array) can be sorted by Collections.sort (or Arrays.sort).
"Comparator Interface"
We need to control the order of a class, which does not support ordering (that is, the comparable interface is not implemented). So, we can create a comparer for this class, which only needs to implement comparator. If a class implements the comparator interface, it must implement the compare (T o1,t O2) method, but it does not necessarily implement the Equals (Object obj) method;
"Comparison between the two"
1.Comparable is a sort interface; If a class implements the comparable interface, it means "this class supports sorting" and comparator is the comparator; if we need to control the order of a class, we can create a "comparer for that class" to sort by.
The 2.Comparable corresponds to an "internal comparator", while the comparator is equivalent to an "external comparator".
Summary of Java two comparators