• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Difference between compare() and compareTo() mtds

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers

Please expalin with example Difference between compare() and compareTo() mtds of Collection.

I try to understand by reading Kathy seira, but not able to get it.

 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that explanation in K&B book is very good.
You have to focus much more and you can get it
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try writing some some code then! Best way. Get things right and you understand. Get things wrong, post your code and then people can really help (your code will give a clue as to what bit you don't understand)

I think of it like this:
compare - compare this to that (it takes two parameters, so it's like a judge in an object beauty contest)
negative answer: this comes before that
positive answer: that comes before this

compareTo - compare me to that (where "me" is the instance who's compareTo method has been called, and it is acting like an egoist, comparing itself to whatever was passed in)
negative answer: i am before than that
positive answer: that is before me

It is common (but not required) for the implementation of a "compare" method to make use of the "compareTo" method on the objects that were passed in.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not getting, can you please expalin with example.
A humble request please....
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"compare" compares two objects passed in as parameters. This is the Comparator interface as is often used to sort sets (like a TreeSet).
"compareTo" compares the instance the method is called on to another object passed in as a parameter. This is the Comparable interface as is often used within "compare".

As for examples, for your own SCJP studies (this is the SCJP forum, after all) it would be much better for you to write your own and then to come back to the forum with questions.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain i have also problem in this topic
please give any small working example
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vineet walia wrote:please explain i have also problem in this topic
please give any small working example



Please read the topic posts so far, then elaborate on what you don't understand. Asking to reexplain something with no specific reason, but I "have also problem" is not very useful.

Henry
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As given in K&B book,

an element in a list can compare itself to another of its own type in only 1 way using its compareTo() method


But a Comparator is external to the element type you'r comparing- its a separate class . So you can make as many of these as you like!



So when using compareTo() method of Comparable interface, the class which calls Collections.sort(<List>) implements Comparable interface and write compareTo() method only once. Hence you can compare your objects only in 1 way.
But if you want to compare your objects by more than one way, then you should use compare() method of Comparator interface.

As given an example in K&B, to compare songs by only title, use compareTo() method.
But if you want to sort songs by its artist or by its title, to implement this, use compare() method.

I hope this clears all your confusion.
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic