• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

hashmap and equals doubts

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have just one question here regarding equals method
please see the output below


if you look at the out put carefully and code you will find that value of this.name changes whenever hashmap put's or get's the value thats the only thing left here i m not getting
can any one explain ?
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naved momin wrote:
if you look at the out put carefully and code you will find that value of this.name changes whenever hashmap put's or get's the value thats the only thing left here i m not getting
can any one explain ?


Hi,

follow this link : http://www.docjar.com/html/api/java/util/Hashtable.java.html
This code is an implementation of HashTable in Java.
Look at these lines, this is a way Hastable.put() is implemented:


Do you see it ?
All your objects have the same hash value.
So HashTable stores them in the same bucket ( tab[ (hash & 0x7FFFFFFF) % tab.length ] ).
Buckets are implemented as simple arrays.
HashTable loops through all elements of this bucked (array) and for each table element calls it's element.equals( key ) method.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

HashTable loops through all elements of this bucked (array) and for each table element calls it's element.equals( key ) method.


thats why "this.name" always changes when i call and "obj.name" remain same ?
 
bacon. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic