Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logical error in creating a HashMap

 
Anil Giri
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a HashMap which string keys and HashSet of Strings as values. I wrote the following code for this.



The file cvtfile.txt has the following content:


The Output is:


I am not able to make out why is the value set that is read at last is getting mapped to all the keys.
I would be grateful for any comments/suggestions.
 
Anil Giri
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

error is in line line 25, change this line into:




 
Anil Giri
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sir!
That corrected it.

So that means the put method of the HashMap class does not copy the object to the map. It just links the object to the map value, so that the last value added the object persists.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that means the put method of the HashMap class does not copy the object to the map.

Yes, it does not copy the object (the content of the object), it only stores the reference to the object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic