This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes hashmap and equals doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "hashmap and equals doubts" Watch "hashmap and equals doubts" New topic
Author

hashmap and equals doubts

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

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 ?

The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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

Joined: Jul 03, 2011
Posts: 688

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 ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashmap and equals doubts
 
Similar Threads
Jtable retreiving data
HashCode and Equals
Employee with same values is being added two times in HashSet
hashcode
hashCode & HashMap doubts