wood burning stoves 2.0*
The moose likes Java in General and the fly likes need help copying hashmaps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "need help copying hashmaps" Watch "need help copying hashmaps" New topic

need help copying hashmaps

gino jersey

Joined: May 24, 2005
Posts: 21
hi all.
i need to have 2 hashmaps so i can write to a log file what the records look like "before" the update.
i have oldHashMap and newHashMap.

my problem is when i update newHashMap, and use OldHashMap to write to the log file, the "new" values are written to the log file.

what's the best way to copy (by value?) the values in oldhashMap to newHashMap.

I tried putAll() but did not work.

Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
I think you would need to manually iterate through the contents of the old hash map, clone each item, and add each clone to the new hash map.

Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Look at cloning.
Your problem says that both hashmaps refer to the SAME set of objects. You need to make clones of the objects in the one hashmap and save the clones in the other.
Consider Paul's rocket mass heater.
subject: need help copying hashmaps
Similar Threads
Reading Report parameters from property file and create a method inside a properties
create an exception
Getting all data in the consol and dumping it into a text file
Memory size and access
how to compare two Hashmap contents.