Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how hashmap identify the duplicate values? (i need internal logic)
 
santhosh kumar uppari
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

santhosh kumar uppari wrote:how hashmap identify the duplicate values? (i need internal logic)

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wiki on hash table / map is great -> http://en.wikipedia.org/wiki/Hash_table
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic