Results 1 to 5 of 5
  1. #1
    Whisperer is offline Member
    Join Date
    Aug 2011
    Location
    Cornwall, UK
    Posts
    25
    Rep Power
    0

    Default Changing the Value of a map

    Map<Integer, Set<String>> a Map = etc....;

    Having declared the map above correctly, if I wish to add a member to the existing Set of strings at a particular key is it sufficient to use:

    Set<String> aSet;
    aSet = aMap.get(Integer anInt);
    aSet.add(aString);

    Is the value of the key/value pairing now modified in aMap?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Changing the Value of a map

    Quote Originally Posted by Whisperer View Post
    Is the value of the key/value pairing now modified in aMap?
    Yup, it is modified in aMap and as a value in the set aSet as well; Java doesn't make any copies of an object when it stores (or retrieves it) in a Collection.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Whisperer is offline Member
    Join Date
    Aug 2011
    Location
    Cornwall, UK
    Posts
    25
    Rep Power
    0

    Default Re: Changing the Value of a map

    Thanks Jos, just to finish it off, I assume that closing aSet does not affect the updated aMap.
    TIA GT

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Changing the Value of a map

    Quote Originally Posted by Whisperer View Post
    Thanks Jos, just to finish it off, I assume that closing aSet does not affect the updated aMap.
    TIA GT
    How do you 'close' a Set? If you 'forget' about a Set while there still exists a reference to it (possibly in another Collection), nothing happens to te Set.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Changing the Value of a map

    Quote Originally Posted by Whisperer View Post
    Thanks Jos, just to finish it off, I assume that closing aSet does not affect the updated aMap.
    TIA GT
    How do you 'close' a Set? If you 'forget' about a Set while there still exists a reference to it (possibly in another Collection), nothing happens to te Set.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Changing 8 to eight...need help!
    By adjit in forum New To Java
    Replies: 31
    Last Post: 06-18-2011, 09:47 PM
  2. changing TF formula
    By o.imen in forum Lucene
    Replies: 0
    Last Post: 09-16-2010, 01:20 PM
  3. Replies: 0
    Last Post: 08-14-2010, 10:09 AM
  4. Changing Cursor
    By ridvan in forum AWT / Swing
    Replies: 2
    Last Post: 06-27-2010, 04:11 AM
  5. Changing the Jframe
    By Nemo1959 in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 03:58 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
  •