Comparable & Comparator are used to implement the comparison and sorting of elements in the collection, except that comparable is the sort of method implementation defined within the collection, Comparator is the sort implemented outside the collection, so, if you want to implement sorting, You need to define a method for the Comparator interface outside of the collection or to implement the comparable interface within the collection.
Comparator is located under Package Java.util, while comparable is under package Java.lang
Comparable is an interface that the object itself has already supported for self-comparison (such as String, Integer itself can do the comparison size operation, has implemented the comparable interface)
Custom classes to be able to sort after joining the list container, you can implement the comparable interface, in the sort method of collections class, if you do not specify comparator, then in a natural order, as the API said: sorts the Specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the comparable interface
The natural order here is to implement the sorting method of the comparable interface settings.
andComparator is a dedicated comparator, and when the object does not support self-comparison or the self-comparison function does not meet your requirements, you can write a comparator to achieve a comparison of the size between the two objects.
It can be said that one is self-completed comparison, one is the difference between the external program implementation comparison.
using Comparator is the strategy mode (strategy design pattern), which does not change the object itself, but uses a policy object (strategy object) to change its behavior.
For example: you want to sort integers by absolute size, integer is not required, you do not need to modify the integer class (in fact, you can not do so) to change its sorting behavior, as long as the implementation of the Comparator interface object to achieve control of its ordering is OK.
abscomparator.java import java.util.*;p ublic class AbsComparator implements Comparator { public int Compare (OBJECT   O1,   OBJECT   O2) { int v1 = math.abs ((Integer) O1). Intvalue ()); int v2 = math.abs ((Integer) O2). Intvalue ()); return v1 > v2 ? 1 : (v1 == v2   ?   0   :   -1); } }
The
can be tested with the following class abscomparator:
Test.java import java.util.*; public class Test { public static void main (String[] args) { //produces an array of 20 random integers (with positive negative) random rnd = new random (); Integer[] integers = new Integer[20]; for (int i = 0; i < integers.length; i++) integers[i] = new integer (Rnd.nextint) * (Rnd.nextboolean () ? 1 : -1)); system.out.println ("Sort by integer built-in method:"); arrays.sort (integers); system.out.println (arrays.aslist (integers)); system.out.println ("Sort by Abscomparator:"); arrays.sort (integers, new Abscomparator ()); &NBSP;&NBSP;&NBsp; system.out.println (arrays.aslist (integers)); } }
Collections.sort (list<t> list, comparator< Super t> C) is used to sort the list.
If you are not calling the sort method, compare the size of the two objects directly, as follows:
Comparator defines two methods, namely
int compare (t O1, T O2) and Boolean equals (Object obj),
Used to compare whether two comparator are equal
True if the specified object is also a comparator and it imposes the same ordering as this comparator.
Sometimes when implementing the comparator interface, the Equals method is not implemented, and the program does not have an error because the class that implements the interface is also a subclass of the object class, and the object class has implemented the Equals method
The comparable interface provides only the int compareTo (T O) method, which means that if I define a person class that implements the comparable interface, then when I instantiate the person1 of the person class, I want to compare person1 and The size of an existing person object Person2, I can call it this way: Person1.comparto (Person2), can be judged by the return value, and at this point if you define a Personcomparator (implements the comparator interface), then you can:
Personcomparator comparator= new Personcomparator ();
Comparator.compare (Person1,person2);
Comparator and comparable in Java