aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help with HashMap 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 "Help with HashMap" Watch "Help with HashMap" New topic
Author

Help with HashMap

Sugantha Jeevankumar
Ranch Hand

Joined: Jun 06, 2007
Posts: 92
While studying Collections I tried this code segment:

With the instanceof test,I could retrieve all the elements including the value with the dog() key and with 2 Integer keys....but when I removed the instanceof test the compiler gives an error as follows:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer canno
t be cast to Dog
at Dog.equals(MyGen1.java:12)
at java.util.HashMap.put(HashMap.java:376)
at MyGen1.main(MyGen1.java:24)

The reasoning behind my removing the test for casting is I thought since Integer classes provide their own implementation for hashcode() and equals()... those methods would be used instead of my implementation.But why does the Dog class' hashcode() and equals() get called?

Please clear my doubt...Thanks in advance...


SCJP 5.0
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The issue is that you are trying to cast an Object to a Dog, but the Object is an Integer.
Sugantha Jeevankumar
Ranch Hand

Joined: Jun 06, 2007
Posts: 92
hey Keith...
The hashcode() and equals() overridden by me should be used only while inserting Dog() objects as key to the collection rite...why are they used for Integers when Integer class has its own implementation of hashcode() and equals()...please clear my doubt
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 507
Hi
I agree to what Keith has mentioned.
You should uncomment the if statement which checks the instanceof Dog stuff.
i.e
//if(o instanceof Dog){

It should work fine after that.
While putting a key-value entry in the HashMap the key's equals would get called to check if the key is already present in the HashMap. This is where Dog's equals() must be getting called and then because the object passed to that method is Integer which can not be type casted to the Dog reference exception is thrown.
Hope this helps
Amit


Regards,
Amit
Sugantha Jeevankumar
Ranch Hand

Joined: Jun 06, 2007
Posts: 92
I am so sorry...I still dont get it
kenny yen
Greenhorn

Joined: Oct 09, 2007
Posts: 7
Hi sugantha,
just to keep it simple. When comparing objects, it checks the hashCode() method to see whether they're the same, and if they're the same, it'll go to the equals() method to compare.
One of the item in the map has got 6 as the key, and your dog object returns 6 in the hashcode as well. Because the hashcode of Integer 6 is 6. So, in this case, it'll go to the equals method that you wrote, but the object that you pass into the equals method is Integer. So, it throws a casting exception. Hope it helps =P
Sandip Sarkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 55
Hi All,

I got the fact that the hashcode method is used to track the correct hash bucket and then we use the equals method to know if they are equal.

But what bugs me is this------

if i remove the comments for instanceof test from the program, when i try to retrieve the value for the Integer key 2 or say 6, it goes to the hash bucket as in this case we have a single hash bucket. now comes the equals() part. An Integer is passed as Object to the method and hence the below condition is bound to fail.


then how do we get the corresponding value from the map??
and additionally if we say that while retrieving we use the Integer class's own equals and hashcode methods then what is the problem in having it commented in the code because herewe are passing dog objects and Integers only??

thanks.
Thanks.
Lucas Lech
Greenhorn

Joined: Dec 10, 2007
Posts: 23
Interesting example.
The ClassCastException is probably thrown during conflict resolution
in hash data structure. I don't know how are the conflicts resolved,
didn't bother to go through the source code but let's assume this
is a List.

Let this list contain two indexed String objects:
[ 6 ; String:"New York" ]
[ Dog ; String:"Canada" ]

Now, if you run get(6) you'll need to iterate this conflict list to
find your key, and since there are *heterogeneous* keys in this list
there is no way you'll get your object unless it's first in this list
or all objects preceding the one you're looking for are of the same type,
namely int - otherwise you'll get ClassCastException on first non-int key.

Try reordering lines of your code to see that in some insertion configurations the code *will* work.

[ December 11, 2007: Message edited by: Lucas Lech ]
[ December 11, 2007: Message edited by: Lucas Lech ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with HashMap
 
Similar Threads
Retrieving data from Maps.
Doubt in collection
Doubt in Hash Map
Collection - Chap 7 (Maps) (Problem in Understanding an example in K&B SCJP 1.6)
Problem faced in using hashset(comparison of equal elements)