• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why here they have used Compare and compareTo both

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In the below code when i sort the array , they are passing to entries of map and comparator, then why do we have compare and compareTo both , what is called for comparing ?
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the compare() method is called since it is the default method of Comparator. Not sure why compareTo() is also written (not overrriden).
 
Jhon Clay
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.. I too have this doubt , method source is method source. and one more doubt , we are sending 2 arguments to the method right, where are we sending it from , How are the entries been taken into compare method for comparision?
 
Greenhorn
Posts: 18
C++ Suse Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jhon Clay wrote:Thanks for the reply.. I too have this doubt , method source is method source. and one more doubt , we are sending 2 arguments to the method right, where are we sending it from , How are the entries been taken into compare method for comparision?



the 2 arguments to compare method come from sort:
- for each pair of elts in arraylist sort will pass them to compare method and order them based on the result os that compare call.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Presumably they have some situations where the operands implement Comparable, when it is easier to use its compareTo method.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jhon Clay wrote:In the below code when i sort the array , they are passing to entries of map and comparator, then why do we have compare and compareTo both , what is called for comparing ?


As others have said: either.

There are two ways to compare objects of similar types (ie, classes):
1. Make the class Comparable. This is done by having it implement the Comparable interface, which defines the compareTo() method.
2. Supply a Comparator, which is an implementation of the Comparator interface, which defines the compare() method.

In the first case, the method is part of the object, so it only needs one argument (the object that this is being compared to). In the second, the Comparator is a completely separate object, so you have to supply both of the objects being compared.

Also: you can only implement Comparable once, so a class can only have one compareTo() order - often called its "natural" order. On the other hand, you can create as many Comparator objects as you like to implement other ones.

I'm a bit worried about your code though. These days, both interfaces use generics, whereas your code doesn't have any at all, and uses casting to ensure success.
If you wrote it yourself, then it's bad practice. If it was written by someone else, then it may be very old and in need of refactoring.

Unfortunately, only you can tell us which.

Winston
 
This tiny ad will self destruct in five seconds.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic