i am copying values from Hashmap to another Hashmap
Hashmap a = b;
That doesn't copy values from one Map to another.
Just like every other reference variable assignment in Java, all that does is make a copy of the reference, so now we have two references pointing to the same HashMap object.
as soon as i put some values in a it atuomatically updates values in b ..
i dont wannt that ..
Any tips on this ?
You need to actually copy the Map.
HashMap has a copy constructor. It's also Cloneable. Either one of those will work.
Note, however, that this is a shallow copy. You'll have two Maps, but the Maps' keys and values references will point to the same objects. So if you do a.put(x, y), it won't affect b, but if you do a.get(x).setY(z), then both Maps will see it, since they're referencing the same value object for the key x.
Joined: Sep 01, 2006
thanks for fast reply .. i will update on this ...