*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Working with HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Working with HashMap" Watch "Working with HashMap" New topic
Author

Working with HashMap

Maya Karthik
Greenhorn

Joined: Jul 18, 2008
Posts: 10
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
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
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());

}

}


kaushik Vira
-------------------------------------
SCJP, Preparing SCWCD..
Maya Karthik
Greenhorn

Joined: Jul 18, 2008
Posts: 10
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
Sunny Mattas
Ranch Hand

Joined: Apr 22, 2008
Posts: 45
[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


Regards
Sunny Mattas
SCJP5
Sunny Mattas
Ranch Hand

Joined: Apr 22, 2008
Posts: 45
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

Joined: Feb 01, 2007
Posts: 102
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.
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
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


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Working with HashMap