aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confusion about comparator, comparable and hashcode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confusion about comparator, comparable and hashcode" Watch "confusion about comparator, comparable and hashcode" New topic
Author

confusion about comparator, comparable and hashcode

Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
Hi,
Can anyone clear this up for me:

The point of using the comparator & comparable interface is to sort objects in collections. Right?

Don't the hashcode/equals methods play a role in sorting in generic collections?

Do you still need to override the hashcode/equals method for the class which implements the comparator/comparable interfaces?

Sometimes I see classes which implement these interfaces, override the appropriate compareTo/compare methods.
Then I see the those classes which implement these interfaces put in as generic types for collections such as TreeSet.
But I don't see where it indicates overriding the hashcode and equals methods.

Thank You


SCJP 6.0
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Equals and hashCode cannot play a role in sorting. This is because they don't tell in any way whether an object is greater or smaller than the other. If you are using a comparable object in a TreeSet or TreeMap, then you don't need to override the equals or hashCode method. The TreeSet and TreeMap uses the compareTo/compare method to check equality (i.e. if compareTo/compare method returns 0, then objects are considered equal)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey Fritz ,
The point of using the comparator & comparable interface is to sort objects in collections. Right?
This is correct.
Don't the hashcode/equals methods play a role in sorting in generic collections
nope, hash code and equals play role in searching not in sorting. As you can see in the below code, equals and hash code has not been overridden still is sorts the ArrayList well.




Do you still need to override the hashcode/equals method for the class which implements the comparator/comparable interfaces?

- You need not to implement hascode and equals if you only want to do sorting not seraching (by verifying logical equal comparision). As you can see the above code it does not find the position of "First name" in the collection because we have not overridden equals method.



Sometimes I see classes which implement these interfaces, override the appropriate compareTo/compare methods.
Then I see the those classes which implement these interfaces put in as generic types for collections such as TreeSet.
But I don't see where it indicates overriding the hashcode and equals methods.


For any collection to be in tree set, it must implement comparable interface so that TreeSet could maintiain its property by keeping the elements in sorted order. It does not matter whether equals and hash code has been implemented or not. (As mentioned in the above code)

I hope this would be help full.

Regards
Salil Verma

Regards
Salil Verma
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confusion about comparator, comparable and hashcode