• 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
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Guess there might have some wrong with you code. cause HashMap not override hashcode() of object class.

so it must have same behavior like object hashcode(). i tried with code given below it`s not giveing any chages.

so better you post your code which you tried out. my code is.

public class TestHashCodeInHashmap {

public static void main(String[] args){
HashMap hi = new HashMap();
hi.put("","");
hi.put("","");
System.out.println(hi.hashCode());

}

}
 
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 Kaushik,

This is the code which i ve been trying.

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());
}

I tried your code changing the objects we put into the map.It gave a diiferent hashCode() then. I think you got the same hashCode() coz
hi.put("","");
hi.put("","");
here the objects we put are same.But How does it work?Coz the key cant be a duplicate one rite?

Thanks,
Maya
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[B][/B]

Hi
I think it is the key whose hash code value really matters when dealing with maps, In your code Integer is the key and String are the values.Hash code increases the efficiency while retrieving values from the hashmap.Whenever one retrieves the values from hashmap the hashcode() is called on each key.If hash code is equal then equals method is called to check the values of key.
In your code it is the hashcode of Integer method which really matters.
Integer class has its own hashcode and equals method, so you don't need to override them .
I don't know why you are calling hash code on hash map object.
I am not sure but it may be calling hashcode method in Object class,
which always returns distinct hash code.

Regards
Sunny
 
Sunny Mattas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok hashCode() you called in you program belongs to class AbstractMap
method has following specifications
public int hashCode()

Returns the hash code value for this map. The hash code of a map is defined to be the sum of the hash codes of each entry in the map's entrySet() view. This ensures that t1.equals(t2) implies that t1.hashCode()==t2.hashCode() for any two maps t1 and t2, as required by the general contract of Object.hashCode.

This implementation iterates over entrySet(), calling hashCode on each element (entry) in the Collection, and adding up the results.

Map class is also having hashcode() method
Regards
Sunny Mattas
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really sorry. That Hashmap is not overriding hashcode() actually i am wrong.

impl of hashcode() is
public int hashCode() {
return (key==NULL_KEY ? 0 : key.hashCode()) ^
(value==null ? 0 : value.hashCode());
}

here you can see hash code value is depending one hash code of key and value.
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kaushik vira:
I am really sorry. That Hashmap is not overriding hashcode() actually i am wrong.

impl of hashcode() is
public int hashCode() {
return (key==NULL_KEY ? 0 : key.hashCode()) ^
(value==null ? 0 : value.hashCode());
}

here you can see hash code value is depending one hash code of key and value.



No, you are right, HashMap doesn't override hashCode() directly, this hashCode is of its static class:

that has this hashCode implematation:

as we can see, HashMap extends AbstractMap:


and it does override hashCode, see it:


I hope it helps,

Kind Regards,
Raphael Rabadan
 
reply
    Bookmark Topic Watch Topic
  • New Topic