• 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

Working with HashMap

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

What does it print when the hashCode() method is called on a Map object?
Each time i put an object to the Map it prints a different value for hashCode(). How is that working?


Thanks,
Maya
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like a beginner's question.

Please explain more; it is not usual to see the hashcode when "putting" pairs into a Map. Are you trying to get the hashcode of the whole Map? Well, of course it would be different every time you change it structurally.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The directory you installed your JDK in will contain a file called src.zip. This contains the source code for the standard Java API. You can look at this to see how each class calculates its hashcode value.
 
Maya Karthik
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ritchie

hashCode() returns a unique value for any object on which it is called rite? I was thinking the same way for map object also. Please see the following code:

String str[]={"abc","111","xyz"};
Map map=new HashMap();
for(int i=0;i<str.length;i++)
{
map.put(new Integer(i),str[i] );
System.out.println(map.hashCode());
}

Here after the put operation it printed different values for hashCode()[It was called on the same object, map].

Quoting you, "Well, of course it would be different every time you change it structurally." How does that happen?? Does it create a new map object when we put a new object into it? Please clarify.

Thanks in advance,
Maya
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Maya Karthik:
hashCode() returns a unique value for any object on which it is called rite?



Not quite. Code that uses a hashcode will work better if all unequal objects returned a different hashcode, but in practice this is impossible (the hashcode is an int so there are only Integer.MAXVALUE possible values it can have).

Originally posted by Maya Karthik:
Quoting you, "Well, of course it would be different every time you change it structurally." How does that happen?? Does it create a new map object when we put a new object into it? Please clarify.



The hashcode is used to try and tell whether two objects are equal (this is different from them being the same object). If the two objects return different hashcodes then they are definitely NOT equal. If they return the same hashcode then they MAY be equal but this is not guaranteed.

Suppose you had two Maps that contained the same objects. It would be reasonable to assume that these Maps could be considered equal, so they may return the same hashcode value. If you then added another object to one of the Maps, they could no longer be considered equal. Now, it is not a requirement that they should now return different hashcodes but it would be good if they did. Therefore when an object is added to a Map it is likely that it will recalculate its hashcode.

As I said earlier, take a look at the source code for some of the classes that implement the Map interface and you will be able to see how they calculate their hashcode and when it is likely to change.
 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Maya
Nice Question which i am not aware that each adding the key/value pair in Hashmap changes the hashcode of map .
In JDK HashMap.java it was written each time when add the key/value pair
it does some re hashing technique to accommodate the new values
even when you invoke remove() re hashing will be done again .

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is correct, but it is a different hashCode from wht we have been discussing earlier.

Earlier we discussed the hashCode of the Map.
What you have is the hashCode of the individual key.
 
Popeye has his spinach. I have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic