File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
santhosh kumar uppari
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how hashmap identify the duplicate values? (i need internal logic)
 
santhosh kumar uppari
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santhosh kumar uppari wrote:how hashmap identify the duplicate values? (i need internal logic)
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wiki on hash table / map is great -> http://en.wikipedia.org/wiki/Hash_table
 
ravindar dayachand
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic