Comparator and comparable are the same

They are both 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, which contains person1, person2, person3,…, we use collections. Sort (personlist),   You can’t get the expected results. At this time, someone must ask, why can you sort a string list:

For example, stringlist {“hello1”, “hello3”, “hello2”}, collections. Sort (stringlist) can be sorted correctly 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.

The difference between comparator and comparable

Comparable

Comparable is defined inside the person class:

Public class person 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 that of string. You can compare sizes anytime and anywhere, because the person itself is divided into sizes. Collections. Sort (personlist) can get the correct results.

Comparator

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

public class Person{ String name; int age },

Then we define another comparator:

Public personcomparator implements comparator() {.. compare the size of persons..},

How to compare the size of two persons is implemented in the person comparator. Therefore, in this way, when we want to sort a personlist,   In addition to passing the personlist, we also need to pass the person comparator, because how to compare the size of people is in the person comparator   Implemented inside, such as:

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

Examples of comparator and comparable

Comparable:

To implement the comparable interface, you need to override 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 string comparison  

If (I = = 0) {/ / if the name is the same, compare the age and return the age comparison result  

return age – another.age; 

} else { 

return i; // If the names are different, the result of comparing names will be returned  







At this time, we can sort them directly with collections. Sort (personlist)

Comparator:

To implement comparator, you need to override 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); // Use string comparison  

If (I = = 0) {/ / if the name is the same, compare the age and return the age comparison result  

return one.age – another.age; 

} else { 

return i; // If the names are different, the result of comparing names will be returned  







Collections. Sort (personlist, new personcomparator()) can sort them

4: Summary

The two methods have their own advantages and disadvantages. Using comparable is simple. As long as the object implementing the comparable interface directly 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 implements another comparator when a user-defined   When the object needs to be compared, the size can be compared by passing the comparator and the object together, and in the comparator, the user can   It has realized complex and universal logic, so that it can match some relatively simple objects, which can save a lot of repetitive work.

Leave a Reply

Your email address will not be published. Required fields are marked *