• 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

Map as a key

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please see the code:



My question goes, Why do all HashMap(s) return same hashCode and obviously what out I get from the get() method (now I can accept this seeing hashCode same for all).

So question remains one, why can't we use Map as a key?

What I understand till now "we need to extend the class (HashMap) and implement hashCode and equals method in that class to get the expected result in the above code"

Is that right way to go with?


Thanks,
cmbhatt
[ May 06, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do all HashMap(s) return same hashCode
Because all three HashMaps hm1, hm2 and hm3 are equal using equals(); that's part of the contract of hashCode().

You can use maps as hash keys, but their concept of equals is not object identity (==). Rather, they are equal if and only if (roughly speaking) they have the same keys, and the keys map to the same values (where same is defined by equals() ). More precisely, see the API:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/AbstractMap.html#equals(java.lang.Object)
http://java.sun.com/j2se/1.5.0/docs/api/java/util/AbstractMap.html#hashCode()

You might want to try putting some different things into hm1, hm2 and hm3, so that they are no longer equal using equals(), and see how that affects your output.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Matt,


Thanks for the quick explanation and links. That is really interesting.



Thanks,
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is the AbstractMap.hashCode(), which uses the hashCode keys to build it. If the keys contains itself, you have an infinite loop.
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excuse me for the previous message, I was in the wrong discussion
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic