File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in K&B SCJP 5: topic Maps 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 "Doubt in K&B SCJP 5: topic Maps" Watch "Doubt in K&B SCJP 5: topic Maps" New topic
Author

Doubt in K&B SCJP 5: topic Maps

Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
page 563:
Remember that when you use a class that implements Map, any classes that you
use as a part of the keys for that map must override the hashCode() and equals() methods.

Is there really a need to overwrite hashCode() when using a TreeMap? I don't see any and the TreeMaps I created to test it all work fine.
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
i think,
you are right there is no need , because there is no hashing involved in case of TreeMap, but as per java contract
if you are overriding equals method, then hash code should also be overridden, this makes sense as in future if you switch to Hashmap from treemap, your code will run as good as it is now


thanks,<br />gaurav abbi
Sri Kan
Ranch Hand

Joined: Feb 20, 2007
Posts: 30
Purpose of overriding hashCode() and equals() methods is to change the behaviour of a class in sorting and comparison.

It is not mandatory to override these methods in all classes when used as key to a map. It depends on requirement. But if you override equals() method then hashCode() for two equal objects must return same value.

Please see Object.hashCode() method in J2SE API documentation.


Cheers,<br /> <br />SCJP 1.5 (83% Huh !)<br />SCWCD 1.4 (81% that's it ??)
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
ok, that makes sense. Thanks!
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Does that mean TreeMap never uses equals(...) and hashCode() methods?

What is equals() and compareTo() compatibility?
I developed a class which implements the Comparable interface as well as overrides the equals(...) and hashCode() method.

I am using TreeMap, it never concerns with equals(...) as I see.
Is is sure the TreeMap never uses equals(...) and hashCode() methods?



Regards,
cmbhatt


cmbhatt
Sri Kan
Ranch Hand

Joined: Feb 20, 2007
Posts: 30
Does that mean TreeMap never uses equals(...) and hashCode() methods?

equals() and hashCode() usage in TreeMap class is different than equals() and hashCode() method usage in the class that is used as a key in a TreeMap.

What is equals() and compareTo() compatibility?

equals() method is to compare two objects for equality and compareTo() is used to compare one object with the another object for order.

I am using TreeMap, it never concerns with equals(...) as I see.
Is is sure the TreeMap never uses equals(...) and hashCode() methods?


Keys in TreeMap must be comparable i.e. either the class used as key must implement comparable interface (referred as class's natural ordering) or supply a comparator (comaprison function) to TreeMap constructor.

Comparing is commonly used for ordering and not necessarily same as equals.

TreeMap cares about comparing keys for ordering purpose.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Sri,


Alright, but your first explanation looks ambiguous to me!
Comparability of objects (that are used as key) is must for TreeSet but if I ask whether TreeSet uses equals(...) and hashCode() or NOT.

What will be the asnwer? And if yes, where?



Thanks,
cmbhatt
Sri Kan
Ranch Hand

Joined: Feb 20, 2007
Posts: 30
Comparability of objects (that are used as key) is must for TreeSet but if I ask whether TreeSet uses equals(...) and hashCode() or NOT.

What will be the asnwer? And if yes, where?


If your question is whether TreeSet uses equals() and hashCode() method of the object added to it then answer is "no".

TreeSet implements Set interface and backed by TreeMap instance. When you add an object to TreeSet comparison is done as below

-- If object class implements comparable then compareTo method is used
-- If a comparator is supplied in the constructor of TreeSet then compare method is used

Alright, but your first explanation looks ambiguous to me!

Yes it is I was discussing equals() and hashCode() for general usage.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Nice Sri Kan,


TreeMap uses Set and HashMap in the back.
It uses Comparable (default) or Comparator what you pass to its constructor.


Thanks,
cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in K&B SCJP 5: topic Maps
 
Similar Threads
HashMap keys
Using record cache and booking method
is it necessary to provide hashCode with equals
How does the hashcode work in a map?
Help on Hashcode