File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 694
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.
I agree. Here's the link:
subject: need help copying hashmaps
It's not a secret anymore!