Results 1 to 3 of 3
  1. #1
    mjdousti is offline Member
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default Strange HashMap behavior

    Hi all,

    In my program, I have a HashMap. It has HashSets of Strings as its keys and PriorityQueues of Strings as its values. When I change the content of one of its keys, it is no longer remain as a member of the HashMap. This seems strange to me, since I do not change the reference of the key. I just change its content. Please take a look at the following snippet:

    Java Code:
    HashMap<HashSet<String>, PriorityQueue<String>> myHashMap=new HashMap<>();
    
    HashSet<String> myHashSet=new HashSet<>();
    myHashSet.add("abc");
    myHashSet.add("mnq");
    myHashSet.add("al;ksghl");
    
    PriorityQueue<String> myPriorityQueue=new PriorityQueue<>();
    myPriorityQueue.add("3h4");
    myPriorityQueue.add("lskdjf");
    
    myHashMap.put(myHashSet, myPriorityQueue);
    
    if(myHashMap.containsKey(myHashSet))
    	System.out.println("Yes!");
    
    myHashSet.remove("abc");
    
    if(myHashMap.containsKey(myHashSet))
    	System.out.println("Yes!");
    Basically, I expect to see two "Yes!"s, indeed, it prints only one. I did a thorough debugging and realized that the reference number for myHashSet doesn't change after removing one of it's members. So, there should be no reason for this program not to print the second "Yes!".

    Any kind of help is really appreciated.
    Last edited by mjdousti; 08-24-2011 at 06:49 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    If you change the value of a key, won't that be a problem? The HashMap uses the hash of the key's content to position the value. If you want to change a the value of a key, perhaps you should remove it, change it and put it back.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    For more information: print out the hashCode for myHashSet after it has been added to the HashMap,
    then again after the remove call.
    Then add back the item removed and print the hashcode again.

    Here is what I see:

    hc=406697253
    hc=406697253
    Yes1!
    hc=406600899 <<<<< after remove
    hc=406697253 <<<<< after added back

Similar Threads

  1. Strange behaviour
    By imadabh in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-11-2011, 03:31 PM
  2. Strange JVM behaviour
    By pjpr in forum Advanced Java
    Replies: 13
    Last Post: 01-03-2011, 07:39 PM
  3. Strange behaviour in serialization
    By Wolverine in forum Networking
    Replies: 0
    Last Post: 05-23-2009, 12:03 PM
  4. AffinedTransform strange behaviour
    By Echilon in forum AWT / Swing
    Replies: 3
    Last Post: 12-11-2008, 09:58 AM
  5. Strange behaviour in swing
    By cbalu in forum AWT / Swing
    Replies: 1
    Last Post: 05-23-2008, 09:23 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
  •