You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by "Mark Hindess (JIRA)" <ji...@apache.org> on 2006/04/24 14:47:05 UTC

[jira] Created: (HARMONY-403) HashMap hashcode ignores values in entries

HashMap hashcode ignores values in entries
------------------------------------------

         Key: HARMONY-403
         URL: http://issues.apache.org/jira/browse/HARMONY-403
     Project: Harmony
        Type: Bug

  Components: Classlib  
    Reporter: Mark Hindess
    Priority: Minor


While it is obviously trivial to create different HashMaps with identical hashCodes, I'd still expect the following test code to pass:

  HashMap map1 = new HashMap(10);
  HashMap map2 = new HashMap(10);
  map1.put("key", "1");
  map2.put("key", "2");
  assertFalse(map1.hashCode() == map2.hashCode());

That is, I'd expect the 'value' associated with "key" to have some impact on the hashCode.  It passes on the reference implementations I tested.


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


[jira] Reopened: (HARMONY-403) HashMap hashcode ignores values in entries

Posted by "Nathan Beyer (JIRA)" <ji...@apache.org>.
     [ http://issues.apache.org/jira/browse/HARMONY-403?page=all ]
     
Nathan Beyer reopened HARMONY-403:
----------------------------------

    Estimated Complexity: Moderate

I'm reopening this, as my fix broke everything. I think I've narrowed it down to the real problem: invalid assumption about Map.Entry.hashCode's implementation.

> HashMap hashcode ignores values in entries
> ------------------------------------------
>
>          Key: HARMONY-403
>          URL: http://issues.apache.org/jira/browse/HARMONY-403
>      Project: Harmony
>         Type: Bug

>   Components: Classlib
>     Reporter: Mark Hindess
>     Assignee: Nathan Beyer
>     Priority: Minor
>  Attachments: fix.hashmap.hashcode.diff, hashmap.hashcodes.should.differ.diff
>
> While it is obviously trivial to create different HashMaps with identical hashCodes, I'd still expect the following test code to pass:
>   HashMap map1 = new HashMap(10);
>   HashMap map2 = new HashMap(10);
>   map1.put("key", "1");
>   map2.put("key", "2");
>   assertFalse(map1.hashCode() == map2.hashCode());
> That is, I'd expect the 'value' associated with "key" to have some impact on the hashCode.  It passes on the reference implementations I tested.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


[jira] Resolved: (HARMONY-403) HashMap hashcode ignores values in entries

Posted by "Nathan Beyer (JIRA)" <ji...@apache.org>.
     [ http://issues.apache.org/jira/browse/HARMONY-403?page=all ]
     
Nathan Beyer resolved HARMONY-403:
----------------------------------

    Resolution: Fixed

I've applied another fix for this, which should now work. Please check that it fixes this issue. Thanks.

> HashMap hashcode ignores values in entries
> ------------------------------------------
>
>          Key: HARMONY-403
>          URL: http://issues.apache.org/jira/browse/HARMONY-403
>      Project: Harmony
>         Type: Bug

>   Components: Classlib
>     Reporter: Mark Hindess
>     Assignee: Nathan Beyer
>     Priority: Minor
>  Attachments: fix.hashmap.hashcode.diff, hashmap.hashcodes.should.differ.diff
>
> While it is obviously trivial to create different HashMaps with identical hashCodes, I'd still expect the following test code to pass:
>   HashMap map1 = new HashMap(10);
>   HashMap map2 = new HashMap(10);
>   map1.put("key", "1");
>   map2.put("key", "2");
>   assertFalse(map1.hashCode() == map2.hashCode());
> That is, I'd expect the 'value' associated with "key" to have some impact on the hashCode.  It passes on the reference implementations I tested.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


[jira] Assigned: (HARMONY-403) HashMap hashcode ignores values in entries

Posted by "Nathan Beyer (JIRA)" <ji...@apache.org>.
     [ http://issues.apache.org/jira/browse/HARMONY-403?page=all ]

Nathan Beyer reassigned HARMONY-403:
------------------------------------

    Assign To: Nathan Beyer

> HashMap hashcode ignores values in entries
> ------------------------------------------
>
>          Key: HARMONY-403
>          URL: http://issues.apache.org/jira/browse/HARMONY-403
>      Project: Harmony
>         Type: Bug

>   Components: Classlib
>     Reporter: Mark Hindess
>     Assignee: Nathan Beyer
>     Priority: Minor
>  Attachments: fix.hashmap.hashcode.diff, hashmap.hashcodes.should.differ.diff
>
> While it is obviously trivial to create different HashMaps with identical hashCodes, I'd still expect the following test code to pass:
>   HashMap map1 = new HashMap(10);
>   HashMap map2 = new HashMap(10);
>   map1.put("key", "1");
>   map2.put("key", "2");
>   assertFalse(map1.hashCode() == map2.hashCode());
> That is, I'd expect the 'value' associated with "key" to have some impact on the hashCode.  It passes on the reference implementations I tested.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


[jira] Resolved: (HARMONY-403) HashMap hashcode ignores values in entries

Posted by "Nathan Beyer (JIRA)" <ji...@apache.org>.
     [ http://issues.apache.org/jira/browse/HARMONY-403?page=all ]
     
Nathan Beyer resolved HARMONY-403:
----------------------------------

    Resolution: Fixed

I've applied the patch at r418844, plus an additional fix. The patch hashCode calculation didn't completely fix the problem; the test case still failed. The problem seemed to be that the 'put' didn't instantiate an Entry correctly on the new key case; it wasn't passing the value to the Entry constructor, so the value was always missed.

Let me know if this resolves the issue properly. Thanks.

> HashMap hashcode ignores values in entries
> ------------------------------------------
>
>          Key: HARMONY-403
>          URL: http://issues.apache.org/jira/browse/HARMONY-403
>      Project: Harmony
>         Type: Bug

>   Components: Classlib
>     Reporter: Mark Hindess
>     Assignee: Nathan Beyer
>     Priority: Minor
>  Attachments: fix.hashmap.hashcode.diff, hashmap.hashcodes.should.differ.diff
>
> While it is obviously trivial to create different HashMaps with identical hashCodes, I'd still expect the following test code to pass:
>   HashMap map1 = new HashMap(10);
>   HashMap map2 = new HashMap(10);
>   map1.put("key", "1");
>   map2.put("key", "2");
>   assertFalse(map1.hashCode() == map2.hashCode());
> That is, I'd expect the 'value' associated with "key" to have some impact on the hashCode.  It passes on the reference implementations I tested.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


[jira] Commented: (HARMONY-403) HashMap hashcode ignores values in entries

Posted by "Mark Hindess (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/HARMONY-403?page=comments#action_12414698 ] 

Mark Hindess commented on HARMONY-403:
--------------------------------------

Nathan, that test case was a very bad example.  This test is better:

    public void test_hashCode() {
        HashMap map = new HashMap(10);
        Integer key = new Integer(1);
        Integer val = new Integer(2);
        map.put(key, val);
        int expected = key.hashCode() ^ val.hashCode();
        assertEquals(expected, map.hashCode());
        key = new Integer(4);
        val = new Integer(8);
        map.put(key, val);
        expected += key.hashCode() ^ val.hashCode();
        assertEquals(expected, map.hashCode());
    }

Since the hashCode for Integer is defined as:

  a hash code value for this object, equal to the primitive int value 
  represented by this Integer object.

and the hashCode for HashMap (actually from AbstractMap) is:

  This implementation iterates over entrySet(), calling hashCode on
   each element (entry) in the Collection, and adding up the results.

then the hashCode value in the above tests are well-defined for any RI.  This tests fails on Harmony.


> HashMap hashcode ignores values in entries
> ------------------------------------------
>
>          Key: HARMONY-403
>          URL: http://issues.apache.org/jira/browse/HARMONY-403
>      Project: Harmony
>         Type: Bug

>   Components: Classlib
>     Reporter: Mark Hindess
>     Priority: Minor
>  Attachments: fix.hashmap.hashcode.diff, hashmap.hashcodes.should.differ.diff
>
> While it is obviously trivial to create different HashMaps with identical hashCodes, I'd still expect the following test code to pass:
>   HashMap map1 = new HashMap(10);
>   HashMap map2 = new HashMap(10);
>   map1.put("key", "1");
>   map2.put("key", "2");
>   assertFalse(map1.hashCode() == map2.hashCode());
> That is, I'd expect the 'value' associated with "key" to have some impact on the hashCode.  It passes on the reference implementations I tested.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


[jira] Updated: (HARMONY-403) HashMap hashcode ignores values in entries

Posted by "Mark Hindess (JIRA)" <ji...@apache.org>.
     [ http://issues.apache.org/jira/browse/HARMONY-403?page=all ]

Mark Hindess updated HARMONY-403:
---------------------------------

    Attachment: hashmap.hashcodes.should.differ.diff

(Oops, attached these in the wrong order but JIRA wont really show this so I should have kept quiet.)

The file "fix.hashmap.hashcode.diff" is a fix that I thought might fix the problem but actually doesn't.  However, it still "smells" like a bug to me.


> HashMap hashcode ignores values in entries
> ------------------------------------------
>
>          Key: HARMONY-403
>          URL: http://issues.apache.org/jira/browse/HARMONY-403
>      Project: Harmony
>         Type: Bug

>   Components: Classlib
>     Reporter: Mark Hindess
>     Priority: Minor
>  Attachments: fix.hashmap.hashcode.diff, hashmap.hashcodes.should.differ.diff
>
> While it is obviously trivial to create different HashMaps with identical hashCodes, I'd still expect the following test code to pass:
>   HashMap map1 = new HashMap(10);
>   HashMap map2 = new HashMap(10);
>   map1.put("key", "1");
>   map2.put("key", "2");
>   assertFalse(map1.hashCode() == map2.hashCode());
> That is, I'd expect the 'value' associated with "key" to have some impact on the hashCode.  It passes on the reference implementations I tested.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


[jira] Updated: (HARMONY-403) HashMap hashcode ignores values in entries

Posted by "Mark Hindess (JIRA)" <ji...@apache.org>.
     [ http://issues.apache.org/jira/browse/HARMONY-403?page=all ]

Mark Hindess updated HARMONY-403:
---------------------------------

    Attachment: fix.hashmap.hashcode.diff

Attaching a test case.


> HashMap hashcode ignores values in entries
> ------------------------------------------
>
>          Key: HARMONY-403
>          URL: http://issues.apache.org/jira/browse/HARMONY-403
>      Project: Harmony
>         Type: Bug

>   Components: Classlib
>     Reporter: Mark Hindess
>     Priority: Minor
>  Attachments: fix.hashmap.hashcode.diff
>
> While it is obviously trivial to create different HashMaps with identical hashCodes, I'd still expect the following test code to pass:
>   HashMap map1 = new HashMap(10);
>   HashMap map2 = new HashMap(10);
>   map1.put("key", "1");
>   map2.put("key", "2");
>   assertFalse(map1.hashCode() == map2.hashCode());
> That is, I'd expect the 'value' associated with "key" to have some impact on the hashCode.  It passes on the reference implementations I tested.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


[jira] Commented: (HARMONY-403) HashMap hashcode ignores values in entries

Posted by "Nathan Beyer (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/HARMONY-403?page=comments#action_12414657 ] 

Nathan Beyer commented on HARMONY-403:
--------------------------------------

According to the Object.hashCode() specification this is not a guaranteed property [1].

# It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. 

Is there some behavior you've found that requires this to be true?

> HashMap hashcode ignores values in entries
> ------------------------------------------
>
>          Key: HARMONY-403
>          URL: http://issues.apache.org/jira/browse/HARMONY-403
>      Project: Harmony
>         Type: Bug

>   Components: Classlib
>     Reporter: Mark Hindess
>     Priority: Minor
>  Attachments: fix.hashmap.hashcode.diff, hashmap.hashcodes.should.differ.diff
>
> While it is obviously trivial to create different HashMaps with identical hashCodes, I'd still expect the following test code to pass:
>   HashMap map1 = new HashMap(10);
>   HashMap map2 = new HashMap(10);
>   map1.put("key", "1");
>   map2.put("key", "2");
>   assertFalse(map1.hashCode() == map2.hashCode());
> That is, I'd expect the 'value' associated with "key" to have some impact on the hashCode.  It passes on the reference implementations I tested.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira