Analysis of the difference between comparator interface and comparable interface in Java _java

Source: Internet
Author: User
Tags comparable

Comparable introduction

Comparable is the sort interface.

If a class implements the comparable interface, it means "this class supports sorting." The class that implements the comparable interface supports sorting, assuming that there is now a list (or array) of objects for the class that implements the comparable interface, the list (or array) can be passed through Collections.sort (or Arrays.sort) to sort.

In addition, the object of the class implementing the comparable interface can be used as a key in an ordered map (such as TreeMap) or as an element in an ordered collection (TreeSet) without specifying a comparer.

1. The same place as Comparator and comparable

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, which contains the Person1, Person2, Persion3 ..., we use Collections.sort (personlist),
is not going to get the expected results. There must be someone to ask, why is it possible to sort a string list:

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

2. The difference between Comparator and comparable

Comparable

Comparable is defined within the person class:

public class Persion implements comparable {. Compare the size of person..},

Since the comparator has been implemented, our person is now an object that can be compared to size, and its comparison function is exactly the same as string, and can be taken anywhere
Compare size, because person now has the size of their own. Collections.sort (personlist) can get the right results.

Comparator

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

public class person{String name int age},

Then we define another comparer:

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

In Personcomparator, how to compare the size of two person is achieved. So, in this way, when we're going to sort a personlist,
In addition to passing the personlist past, we also need to pass the personcomparator past, because how to compare the size of the person is in Personcomparator
To implement in the inside, as:

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

3. Examples of Comparator and comparable

Comparable:

Implementation of the comparable interface to cover the CompareTo method, in the CompareTo method to implement the comparison:

public class person implements comparable {
String name;
int age;
public int CompareTo (person another) {
int i = 0;
i = Name.compareto (another.name); Use a comparison of strings if
(i = = 0) {//If the name is the same, compare the age, return the comparison age result returns
age-another.age;
} else {
i;//name is different Sample, return the result of the comparison name.}}

Then we can sort them directly with Collections.sort (Personlist).

Comparator:

Implementing comparator requires overriding compare methods:

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 comparison of strings if
(i = = 0) {//If the name is the same, compare the age, return the comparison of the age result returns
one.age-another.age;
} else {
i;//Name not , return the result of the comparison name
.
}} Collections.sort (Personlist, New Personcomparator ()) can be sorted

4: summary

Two methods have advantages and disadvantages, with comparable simple, as long as the implementation of comparable interface objects directly become a can be compared to the object,
But you need to modify the source code, the advantage of using comparator is that you do not need to modify the source code, but also implement a comparer, when a custom
Objects need to be compared, the comparator and object passed together in the past can be larger than the size, and in the comparator users can
It is possible to achieve complex and universal logic so that it can match some of the simpler 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.