The difference between the comparator interface and the Comparable Interface

Source: Internet
Author: User

The difference between the comparator interface and the Comparable Interface

1. Where Comparator and Comparable are the same

They are all java interfaces and are used to compare the size of custom classes,

What is a custom class: for example, public class Person {String name; int age }.

When we have such a personList that contains person1, person2, persion3...., we use Collections. sort (personList ),
There is no expected result. Someone must ask, why can I sort a string list:

For example, StringList {"hello1", "hello3", "hello2"}, Collections. sort (stringList) can be correctly sorted, because
The String object has helped us implement the Comparable interface, so if we want to sort the Person, we also need to implement a comparator.

2. Differences 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 with a comparable size. Its Comparison function is the same as that of String and can be used anytime, anywhere.
Compare the size, because the Person itself has different sizes. Collections. sort (personList) can get the correct result.

Comparator

Comparator is defined outside the Person class. At this time, the structure of our Person class does not need to be changed, as shown in

Public class Person {String name; int age },

Then we define another comparator:

Public PersonComparator implements Comparator () {... compares the size of Person ..},

How to compare the size of two persons is implemented in PersonComparator. Therefore, when we want to sort a personList,
In addition to passing the personList in the past, we also need to pass the PersonComparator in the past, because how to compare the size of the Person in the PersonComparator
Implementation in, such:

Collections. sort (personList, new PersonComparator ()).

3. Comparator and Comparable Instances

Comparable:

To implement the Comparable interface, you must overwrite the compareTo method and 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); // use the string to compare if (I = 0) {// if the name is the same, compare the age, return the Compare age result return age-another. age ;}else {return I; // results of comparing names are returned if the names are different .}}}
In this case, we can directly use Collections. sort (personList) to sort it.

Comparator:

To implement Comparator, You need to override 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); // use a string to compare if (I = 0) {// if the name is the same, compare the age, return the Compare age result return one. age-another. age ;}else {return I; // results of comparing names are returned if the names are different .}}}
Collections. sort (personList, new PersonComparator () can be sorted

4: Conclusion
The two methods have their own advantages and disadvantages. Comparable is simple. As long as the object implementing the Comparable interface becomes a Comparable object,
However, you need to modify the source code. The advantage of using Comparator is that you do not need to modify the source code, but implement another Comparator.
When the object needs to be compared, the Comparator and object can be passed together to compare the size, and in the Comparator, the user can
Complicated universal logic has been implemented so that it can match some simple objects, which can save a lot of repetitive work.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.