Comparable<t>: Comparable is a comparer inside a class that is used to create a classthe timeTo implement this interface while implementing the comparison method, and for classes that cannot modify the source code, this method cannot be used for comparison sorting, and so on. The source code is:
1 Public Interface Comparable<t> {2public int compareTo (T o); 3 }
You can use comparisons such as Arrays.sort (t[] a) or collections.sort (list<t> list) to compare sorts using the methods defined in the classes that implement Comparable<t>. Second, Comparator<t>: Comparator is a separate comparator that can be used for any class (the class that cannot modify the source code) to sort the class instances by comparison. The source code is:
Public Interface Comparator<t> { int compare (t O1, T O2); Boolean equals (Object obj);}
Can be used such as Arrays.sort (t[] A, comparator<? super t> C) or Collections.sort (list<t> List, comparator<? Super T> c) To use the class instances that implement Comparator<t> as comparators for comparison sorting.
Java Learning notes--comparable & Comparator