I am trying to create 2 hashmaps that start with the same data... I then want to update the data in one of them so that at a later time i can compare and "do stuff".
My problem is evertime I change the data in one it changes the data in the other... I have googled the hell out of this and i cant figure out why. I tires this with the clone() as well buti had hoped that this would work...
anywya can someone please help with this.
//returns the same thing in different lists
List<Guest> guestList = getGuestList();
List<Guest> newGuestList = getGuestList();
//get the hashmaps
HashMap<MeetingInfo, Meetings> a = guestList.get(0).getMeetings();
HashMap<MeetingInfo, Meetings> b = newGuestList.get(0).getgetMeetings();
for (Map.Entry<MeetingInfo, Meetings> aa : a.entrySet())
MeetingInfo akey = aa.getKey();
Meetings avalue = aa.getValue();
for (Map.Entry<MeetingInfo, Meetings> bb : b.entrySet())
MeetingInfo bkey = bb.getKey();
Meetings bvalue = bb.getValue();
//on a match change the room
if(avalue.getLineNumber().compareTo(bvalue.getLineNumber()) == 0 )
//try to update room in the second hashmap only
Both hashmaps likely contain as values references to the same objects. If you're adding arrays to the maps are you deep copying the arrays first before adding to the second?
i doubt I am as I have no idea how to deep copy..
would it be better to clone the first one?
Object copy - Wikipedia, the free encyclopedia
Essentially, you need to make copies or clones of the objects in your HashMap. Simply creating a second HashMap is not enough.