Comparable Introduction
Comparable is the sorting interface.
If a class implements the Comparable interface, it means "this class supports sorting". Since classes that implement Comparable interface support sorting, assuming that there is now a "List list (or array) of objects of classes that implement Comparable interface", the List list (or array) can be sorted by Collections.sort (or Arrays.sort).
In addition, "Object of the class that implements the Comparable interface" can be used as a key in the "ordered map (such as TreeMap)" or an element in the "ordered set (TreeSet)" without specifying a comparator.
1. Comparator and Comparable are the same
They are all Java interfaces, and are used to compare sizes of custom classes.
What is a custom class: such as public class Person{ String name; int age }.
When we have such a personList, which contains person1, person2, person3......, we use Collections.sort( personList ),
The expected result is not obtained. Someone must ask at this time, why can we sort a string list:
For example, StringList{"hello1" , "hello3" , "hello2"}, Collections.sort( stringList ) can get the correct sorting, that's because
The String object has helped us implement the Comparable interface, so if our Person wants to sort, we also need to implement a comparator.
2. The difference between Comparator and Comparable
Comparable
Comparable is defined inside the Person class:
public class Persion implements Comparable {..Compare the size of Person...},
Because the comparator has been implemented, our Person is now an object that can compare sizes. Its comparison function is exactly the same as String, and it can be compared at any time and anywhere, because Person now has a different size. Collections.sort(personList) can get the correct result.
Comparator
Comparator is defined outside of Person. At this time, the structure of our Person class does not need to change any changes, such as
public class Person{ String name; int age },Then we define another comparator:
public PersonComparator implements Comparator() {..Compare the size of Person...}, How to compare the sizes of two Persons in PersonComparator. So, using this method, when we want to sort a personList,
In addition to passing personList, we also need to pass PersonComparator, because how to compare Person size is in PersonComparator
Implemented in, such as:
Collections.sort( personList , new PersonComparator() ).
3. Examples of Comparator and Comparable
Comparable:
To implement the Comparable interface, you must override the compareTo method. Implement comparison in the compareTo method:
public class Person implements Comparable {String name;int age;public int compareTo(Person another) {int i = 0;i = name.compareTo(another.name); // Comparison using strings if(i == 0) { // If the names are the same, compare age, return age result return age - another.age;} else {return i; // The names are different, return the result of comparing names.}}}At this time, we can directly sort it with Collections.sort( personList ).
Comparator:
Implementing Comparator requires overriding the compare method:
public class Person{String name;int age;}class PersonComparator implements Comparator<Person> { public int compare(Person one, Person another) {int i = 0;i = one.name.compareTo(another.name); // Comparison using strings if(i == 0) { // If the name is the same, compare age, return the result of comparing age return one.age - another.age;} else {return i; // The names are different, return the result of comparing names.}}}Collections.sort( personList , new PersonComparator()) It can be sorted4: Summary
Both methods have their own advantages and disadvantages. It is simple to use Comparable. As long as the object that implements the Comparable interface becomes a comparable object,
However, the source code needs to be modified. The advantage of using Comparator is that it does not need to modify the source code, but also implements a comparator. When a custom object needs to be compared, the comparator and the object can be passed together to compare the size. In Comparator, users can implement complex and general logic by themselves, so that they can match some relatively simple objects, which can save a lot of repetitive labor.