Home java What is the difference between comparable from Comparator?

What is the difference between comparable from Comparator?

Author

Date

Category

When to use comparable, and when Comparator?


Answer 1, Authority 100%

Classes implement comparable so that you can then be sorted by the implementation of COMPARETO (OBJECT) method.

If the class implements this interface, then you can use the collection.sort () or arrays.sort () . Objects will be sorted based on the implementation of COMPARETO (OBJECT) method.

For example:

Public Class Country Implements Comparable & LT; Country & gt; {
    @Override
    Public Int CompareTo (Country Country) {
    Return (this.countryid & lt; country.countryid)? -1: (this.countryid & gt; country.countryiD)? 1: 0;
    }
}

When calling collection.sort () On the collection of objects of this class, they will compare based on COMPARETO (Country Country) .

and Comparator is used to implement sorting by custom field, type:

list & lt; country & gt; ListOfcountries = New ArrayList & LT; Country & GT; ();
[...]
Collections.Sort (ListOfcountries, New Comparator & LT; Country & GT; () {
   @Override
   Public Int Compare (Country O1, Country O2) {
     Return O1.getCountryName (). CompareTo (O2.GetcountryName ());
   }
});

The objects will be sorted based on the comparison of the names of the countries.

if we summarize, then:

comparable is implemented inside the class. In fact, determines the usual / natural order of comparing objects.

compararator is implemented outside the class. You can implement various sorting options based on the comparison of various fields.


Answer 2

comparable – Improgram by the class itself, when the natural order of sorting is needed. Example – class string.

Comparator – Imprbentis by other classes. It makes it possible to separate the implementation of the comparison of the class and make several comparison implementations for different parameters for one class.

| Comparable & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | Comparator.
Package & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | java.lang & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | java.util
Functional Interface | YES & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | Yes
Method & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | int o1.compareto (O2) & NBSP; & nbsp; | int compararator.compare (O1, O2)
-1 IF & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | O1 & LT; O2 & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | SAME.
1 IF & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | O1 & GT; O2 & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | SAME.
0 IF & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | O1 == O2 & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | SAME.
IMPLEMENT & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | Class ItSelf & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; | Separate Classes.
Defines & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | Natural Ordering & NBSP; & nbsp; & nbsp; | Sorting by Different Parameters
EXAMPLES & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; | String, Date, Wrappers | For Third-Party Classes

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions