Sunnyamy the difference between comparator interface and comparable interface

Source: Internet
Author: User
Tags comparable

1. Comparator and comparable the same place

They are all Java interfaces, and are used to compare the size of a custom class,

What is a custom class: such as public class person{String name, int age}.

When we have such a personlist, it contains Person1, Person2, Persion3 ...., we use Collections.sort (personlist),
is not getting the expected results. At this point someone must ask, why you can sort a string list:

such as stringlist{"Hello1", "Hello3", "Hello2"}, Collections.sort (stringlist) to get the right sort, that's because
String This object has already implemented the comparable interface for us, so we need to implement a comparator if we want to sort.

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 a comparable size object, and its comparison function is exactly the same as the string, can be taken anywhere
Compare size, because person is now the size of the point. Collections.sort (personlist) can get the correct results.

Comparator

Comparator is defined outside the person, at which point the structure of our person class does not need to change, as

public class person{String name; int age},

Then we define a different comparator:

Public Personcomparator implements Comparator () {.. Compare the size of person:},

In Personcomparator, we realized how to compare the size of two person. So, in this way, when we're going to sort a personlist,
We have to pass personlist in the past, but also need to pass the personcomparator, because how to compare the size of the person is in the Personcomparator
Implemented in the inside, such as:

Collections.sort (Personlist, New Personcomparator ()).

3. Examples of Comparator and comparable

Comparable:

To implement the comparable interface to overwrite the CompareTo method, implement the 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); Using a string comparison
if (i = = 0) {//If the name is the same, compare the age, return the comparison age result
return age-another.age;
} else {
return i; The name is different and returns the result of the comparison name.
}
}
}
We can then sort them directly with Collections.sort (Personlist).

Comparator:

Implementing comparator requires overriding the Compare method:
public class person{
String name;
int Age
}

Class Personcomparator implements Comparator {
public int compare (person one, person another) {
int i = 0;
i = One.name.compareTo (another.name); Using a string comparison
if (i = = 0) {//If the name is the same, compare the age, return the comparison age result
return one.age-another.age;
} else {
return i; The name is different and returns the result of the comparison name.
}
}
}
Collections.sort (Personlist, New Personcomparator ()) can be sorted

4: summary

Each of the two methods have advantages and disadvantages, with comparable simple, as long as the realization of the comparable interface object directly becomes a comparable object,
But the source code needs to be modified, the advantage of using comparator is that you do not need to modify the source code, but also implement a comparator, when a custom
Object needs to be compared, the comparator and the object are passed together in the past can be compared to the size, and in the comparator inside the user can self-
To achieve complex and common logic, so that it can match some relatively simple objects, so that can save a lot of duplication of work.

Sunnyamy the difference between comparator interface and comparable interface

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.