Results 1 to 5 of 5

Thread: HashMap help

  1. #1
    BobswYourUcle is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default HashMap help

    Hello,

    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.


    Java Code:
    //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[0].getLineNumber().compareTo(bvalue[0].getLineNumber()) == 0 )
    		{
    		//try to update room in the second hashmap only
    			bvalue[0].setRoom('L');
    			b.put(bkey, bvalue);
    			
    		}    			
    	}
    }
    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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?

  3. #3
    BobswYourUcle is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    thanks,
    i doubt I am as I have no idea how to deep copy..
    Thanks

  4. #4
    BobswYourUcle is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    would it be better to clone the first one?

  5. #5
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    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.

Similar Threads

  1. Hashmap to TXT and TXT to Hashmap
    By elvinny in forum Advanced Java
    Replies: 4
    Last Post: 02-16-2011, 11:12 PM
  2. Something like HashMap
    By BigBear in forum New To Java
    Replies: 4
    Last Post: 03-14-2010, 11:08 AM
  3. Replies: 7
    Last Post: 12-08-2009, 07:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •