• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

compare and compareTo

 
Ranch Hand
Posts: 61
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the compare method in Comparator take 2 arguments while the compareTo method in Comparable takes one parameter as this argument and another argument is passed to the method?

Is the compare method in Comparator a static method? Is that the reason for accepting 2 arguments?
 
Ranch Hand
Posts: 177
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suhaas Parekh wrote:Why does the compare method in Comparator take 2 arguments while the compareTo method in Comparable takes one parameter as this argument and another argument is passed to the method?

Is the compare method in Comparator a static method? Is that the reason for accepting 2 arguments?



Comparable and Comparator are two interfaces in the Java language, Comparable is used on objects that are naturally ordered, furthermore, Comparator sorting takes into account objects of two different classes

1-Comparable
it uses the compareTo() method for sorting, and this is the only method in the interface, it takes an object and compares it to an object of the same type, i.e if you want to compare a String object to compare you must compare it to another object of the same type, in this case, will be String object



further reading here is Comparabledocumentation provided by Oracle


2-Comparator
There were two methods in this interface, compare() and equals() (Comparator overrides it from Object class and you can use it in all classes as well as clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait)
compare() method takes in two objects as arguments, it returns an integer to indicate how the first parameter compares with the second



further reading, you can visit the documentation provided by Oracle

Comparator

3-Is the compare method in Comparator a static method?

No, both interfaces provide non-static methods, the method can accept various arguments even is not a static method, the static keyword has their own specialization
static keywords applied to variables, nested classes,  methods, and blocks

The static can be:

Variable (also known as a class variable)
Method (also known as a class method)
Block
Nested class

further reading, Static keyword
 
Marshal
Posts: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this Java™ Tutorials section, which I think will help.
 
Campbell Ritchie
Marshal
Posts: 74054
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compareTo() method does take two references. One is the reference it is called on, and the other that passed as an argument.
 
Suhaas Parekh
Ranch Hand
Posts: 61
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the compare method in the Integer class override the one declared in the Comparator?

From the doc, I see that Integer class implements the Comparable interface not the Comparator.
 
Marshal
Posts: 22457
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The compareTo() method does take two references. One is the reference it is called on, and the other that passed as an argument.


Correct. It's easy to create a Comparator for a Comparable class because both require two references. For instance, to create a Comparator<Integer> that delegates to Integer.compareTop, all I need to do (apart from using Comparators.naturalOrder() is to use a method reference:
This is in fact how Comparator.naturalOrder() is implemented (the body, not the lambda; it uses a singleton instance).
 
Suhaas Parekh
Ranch Hand
Posts: 61
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well if compare and compareTo can be used interchangeably, why is the example below not working?

 
Rob Spoor
Marshal
Posts: 22457
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suhaas Parekh wrote:Does the compare method in the Integer class override the one declared in the Comparator?

From the doc, I see that Integer class implements the Comparable interface not the Comparator.


You're still mixing the two concepts.

Comparable means that each instance of a class can compare itself against any other instance of the same class. For instance, I can compare "hello" to "world"; the result indicates that "hello" is smaller than "world".

A Comparator isn't integrated into the class to compare. It's instead an outsider - it compares instances of completely unrelated classes. For instance, even though String is Comparable and I cannot change its compareTo implementation, I can create a Comparator that compares String instances based on their lengths:

(Note that Integer.compare is a better alternative for comparing two ints; it takes care of overflow issues. For String lengths that shouldn't be an issue though, as these are never negative.)

Sometimes you see classes with a static Comparator field; String.CASE_INSENSITIVE_ORDER is an example. This Comparator is still just a Comparator; it could just as easily have been a top-level class:
 
Rob Spoor
Marshal
Posts: 22457
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suhaas Parekh wrote:Well if compare and compareTo can be used interchangeably, why is the example below not working?


Because you're using Comparable, not Comparator. A Comparable has method compareTo that takes just one argument. Integer.compare takes two ints. If you use Comarator instead of Comparable, the line with c1 will compile.

Your other line does work because it calls the compareTo method on an instance, and takes only one argument. That 20 is auto-boxed to an Integer instance.
 
Rob Spoor
Marshal
Posts: 22457
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that I never said that compare and compareTo are interchangeable. compare belongs to Comparator and takes two arguments. compareTo belongs to Comparable and takes an instance to compare and another argument. What I said is that you can easily create a Comparable based on a Comparable.
 
Saloon Keeper
Posts: 1327
40
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Note that I never said that compare and compareTo are interchangeable. compare belongs to Comparator and takes two arguments. compareTo belongs to Comparable and takes an instance to compare and another argument. What I said is that you can easily create a Comparable based on a Comparable.



What I think Rob meant there was:

Rob corrected wrote:What I said is that you can easily create a Comparator based on a Comparable.



To continue to remember which is which even when jumping around from language to language and not doing any comparisons for a long time, I always remembered Comparator as being short for "Custom Comparator".

When there are multiple different ways to possibly compare two objects of some class T, and one is considered natural or normal enough to implement Comparable<T>, but you may have good reason to sometimes sort them in other ways, you can use "Custom Comparators" to obtain those variant orderings where you need them.

I used to sometimes forget the difference between Comparable<T> and Comparator before that, now I never do.

Having more experience now, I think I wouldn't forget either anymore because I recognize that Comparable, like Serializable or Cloneable or Closeable denotes an inherent property of a class that implements it.  I still say "Custom Comparator" in my head tho, and I haven't confused them for a very long time.

 
Rob Spoor
Marshal
Posts: 22457
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:To continue to remember which is which even when jumping around from language to language and not doing any comparisons for a long time, I always remembered Comparator as being short for "Custom Comparator".

When there are multiple different ways to possibly compare two objects of some class T, and one is considered natural or normal enough to implement Comparable<T>, but you may have good reason to sometimes sort them in other ways, you can use "Custom Comparators" to obtain those variant orderings where you need them.


I kinda like that term.

I also have a good example for your Comparable vs. Comparator statement. String is Comparable<String> - you can compare any String with any other String, and it uses case-sensitive, lexicographically comparison. Sometimes you want to sort case insensitively. There is a custom Comparator for that - String.CASE_INSENSITIVE_ORDER. It's a static field of class String that implements Comparator<String>.

There is also one other reason to define a Comparator. If you get an existing class that does not implement Comparable, and you cannot make it implement Comparable (perhaps because it comes from a library), you can still sort instances by defining a custom Comparator.
 
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP

your topic was mentioned in the July 2021 Journal. Have a cow for raising the subject!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic