wood burning stoves*
The moose likes Other JSE/JEE APIs and the fly likes Logical error in creating a HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Logical error in creating a HashMap" Watch "Logical error in creating a HashMap" New topic
Author

Logical error in creating a HashMap

Anil Giri
Greenhorn

Joined: Jun 04, 2009
Posts: 9
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.


SillyCon!
Anil Giri
Greenhorn

Joined: Jun 04, 2009
Posts: 9
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Hi,

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




Anil Giri
Greenhorn

Joined: Jun 04, 2009
Posts: 9
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

Joined: Jun 21, 2008
Posts: 423
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Logical error in creating a HashMap
 
Similar Threads
HashSet unique values
Regarding reading a file in a jsp
Reading one line from a text
to read lines inside brackets
Text to speech