aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how hashmap identify the duplicate values? (i need internal logic) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how hashmap identify the duplicate values? (i need internal logic)" Watch "how hashmap identify the duplicate values? (i need internal logic)" New topic
Author

how hashmap identify the duplicate values? (i need internal logic)

santhosh kumar uppari
Greenhorn

Joined: Nov 15, 2010
Posts: 2
how hashmap identify the duplicate values? (i need internal logic)
santhosh kumar uppari
Greenhorn

Joined: Nov 15, 2010
Posts: 2
santhosh kumar uppari wrote:how hashmap identify the duplicate values? (i need internal logic)
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

I can ensure these points only if you have properly implemented the hashCode-Contract and evaluated the hashCode using the fields used to determine the equality:

1. When you try to put <key,object> pair in a hashmap, a hashCode is generated corresponding to the key.
2. If that hashCode has not been already generated for some previous key of any <key,object> pair, a new <Key,object> pair is added to the hashmap.
3. If the hashCode code has been already generated, then hashCode() method internally calls the equals() method to check whether this new object is identical with the old object that is already in the map(with same key).
--> If equals() returns true: then the new object overwrites the previous object in the hashmap.(i.e. no change in hashMap size)
--> If equals() returns false: then a new object will be added to hashmap with the same key(i.e. size of hashmap will increase by one)

I think this is how <key,object> pairs are put into a hashMap..,

But things are not very much clear to me what happens actually when we retrieve a particular object from a bucket with multiple objects(i.e.objects with same key). If somebody knows please let me know...

Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

When you try to put <key,object> pair in a hashmap, a hashCode is generated corresponding to the key.


By the key you mean ?

If that hashCode has not been already generated for some previous key of any <key,object> pair, a new <Key,object> pair is added to the hashmap.


Equality of hashCode is different from the equality of an object

If the hashCode code has been already generated, then hashCode() method internally calls the equals() method to check whether this new object is identical with the old object that is already in the map(with same key).


huh ? hashCode() is implemented by the custom object. A hash is generated from the hashCode. Equality checks are conducted by the HashMap and not the hashCode() method.

But things are not very much clear to me what happens actually when we retrieve a particular object from a bucket with multiple objects(i.e.objects with same key).


Objects in the same bucket share the same hash. They are maintained as a linked list and the equals() method is used to find the correct value


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

hello Deepak, can you give me a link to this topic...(and from key I mean an object corresponding to which the hashCode is generated)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

Wiki on hash table / map is great -> http://en.wikipedia.org/wiki/Hash_table
ravindar dayachand
Greenhorn

Joined: May 08, 2009
Posts: 14
Hash code makes your retrieval faster, that is the reason for which we use hashcode.

When there are multiple object in a bucket they are compared based on the equals method and the corresponding value is returned and remember each key will have only one value associated.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how hashmap identify the duplicate values? (i need internal logic)
 
Similar Threads
Help with Data Structures...
HashMap sorting
reverse lookup in hashmap
How to find duplicate values in ArrayList with out iterarting..?
how to remove duplicate values in hash map?