• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with HashMap

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The issue is that you are trying to cast an Object to a Dog, but the Object is an Integer.
 
Sugantha Jeevankumar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sugantha Jeevankumar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am so sorry...I still dont get it
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic