• Post Reply Bookmark Topic Watch Topic
  • New Topic

query about comparator  RSS feed

 
ujwwala tem
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I read the below statement in sun's forum, I am not getting the meaning exactly,
Since every class subclasses Object at some point, it is not a requirement that you implement the equals() method. In fact, in most cases you won't. Do keep in mind the equals() method checks for equality of Comparator implementations, not the objects being compared.

When comparing the objects don't we check for equality,

Please guide,

Regards,
Ujwwala
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please quote the link to the Sun/Oracle forum.

It takes a bit of thought to understand, but it's actually quite simple.What you are doing here is testing whether the ob reference is a Comparator<T> which does the same thing as this Comparator<T>. There are some classes which have their own Comparator<T> objects, for example this one. If you are sorting and sorting again with different Comparators<T>, it may enhance performance if the JVM can see that they are both doing the same thing. So you can override the equals method to see whether you are using the same Comparator<T> twice.

Often you think you don't need to do that, because you have written all the Comparators<T> yourself, all doing something different, so you can simply leave the equals() method to the un-overridden one from java.lang.Object.
 
Radhakrishna Sharma Gorenta
Ranch Hand
Posts: 63
Google App Engine Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ujwala,
The main purpose of Comparator is to implement customized sorting. Of course the equal method can be overridden in you class also, as you wish. But the thing is we generally focus on Customized sorting by using Comparator.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!