You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by "Paulex Yang (JIRA)" <ji...@apache.org> on 2006/03/17 08:18:13 UTC

[jira] Created: (HARMONY-206) HashMap's behavior is non-compliant with RI when key is reused

HashMap's behavior is non-compliant with RI when key is reused
--------------------------------------------------------------

         Key: HARMONY-206
         URL: http://issues.apache.org/jira/browse/HARMONY-206
     Project: Harmony
        Type: Bug
  Components: Classlib  
    Reporter: Paulex Yang
    Priority: Minor


As discussion in mailing list,  when hash key is reused by modify its hashcode, Harmony's HashMap show different behavior with RI.  There are some ways to mimic RI's behavior, including rehash the hash of key, and cache the hash when key/value pair is put into. I will attach patches soon.

Test case:
public class HashMapTest { 
public static void main(String[] args) { 
	ReusableKey k = new ReusableKey(); 
	HashMap map = new HashMap(); 
	k.setKey(1); 
	map.put(k, "value1"); 

	k.setKey(18); 
	System.out.println(map.get(k)); 

	k.setKey(17); 
	System.out.println(map.get(k)); 
	} 
} 

class ReusableKey{ 
	private int key = 0; 
	public void setKey(int key){ 
		this.key = key; 
	} 
	public int hashCode(){ 
		return key; 
	} 
	public boolean equals(Object o){ 
		if(o == this){ 
			return true; 
		} 
		if(!(o instanceof ReusableKey)){ 
			return false; 
		} 
		return key == ((ReusableKey)o).key; 
	} 
} 

RI 5.0 output:
  null 
  null 
Harmony output:
  null 
  value1 

-- 
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-206) HashMap's behavior is non-compliant with RI when key is reused

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


I'm reopening this as the patch for this issue cause HARMONY-406. I've applied a new fix at r419085 that should address both issues (passes all regression tests) Please check it out and resolve this issue again.

> HashMap's behavior is non-compliant with RI when key is reused
> --------------------------------------------------------------
>
>          Key: HARMONY-206
>          URL: http://issues.apache.org/jira/browse/HARMONY-206
>      Project: Harmony
>         Type: Bug

>   Components: Classlib
>     Reporter: Paulex Yang
>     Assignee: Tim Ellison
>     Priority: Minor
>  Attachments: java.util.patch, tests.api.java.util.patch
>
> As discussion in mailing list,  when hash key is reused by modify its hashcode, Harmony's HashMap show different behavior with RI.  There are some ways to mimic RI's behavior, including rehash the hash of key, and cache the hash when key/value pair is put into. I will attach patches soon.
> Test case:
> public class HashMapTest { 
> public static void main(String[] args) { 
> 	ReusableKey k = new ReusableKey(); 
> 	HashMap map = new HashMap(); 
> 	k.setKey(1); 
> 	map.put(k, "value1"); 
> 	k.setKey(18); 
> 	System.out.println(map.get(k)); 
> 	k.setKey(17); 
> 	System.out.println(map.get(k)); 
> 	} 
> } 
> class ReusableKey{ 
> 	private int key = 0; 
> 	public void setKey(int key){ 
> 		this.key = key; 
> 	} 
> 	public int hashCode(){ 
> 		return key; 
> 	} 
> 	public boolean equals(Object o){ 
> 		if(o == this){ 
> 			return true; 
> 		} 
> 		if(!(o instanceof ReusableKey)){ 
> 			return false; 
> 		} 
> 		return key == ((ReusableKey)o).key; 
> 	} 
> } 
> RI 5.0 output:
>   null 
>   null 
> Harmony output:
>   null 
>   value1 

-- 
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-206) HashMap's behavior is non-compliant with RI when key is reused

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

    Resolution: Fixed

Thanks Paulex,

I've used a modified version of your patches.  I moved the hashCode() call into the Entry conbtrstructor rather than on each call-site, and put the regression test into the get(Object) method.

Fixed in LUNI module java.util.HashMap at repo revision 389179
.
Please take a look and check that this fully resolves your problem.

(I expect that we have the same issue with Hashtable)

> HashMap's behavior is non-compliant with RI when key is reused
> --------------------------------------------------------------
>
>          Key: HARMONY-206
>          URL: http://issues.apache.org/jira/browse/HARMONY-206
>      Project: Harmony
>         Type: Bug
>   Components: Classlib
>     Reporter: Paulex Yang
>     Assignee: Tim Ellison
>     Priority: Minor
>  Attachments: java.util.patch, tests.api.java.util.patch
>
> As discussion in mailing list,  when hash key is reused by modify its hashcode, Harmony's HashMap show different behavior with RI.  There are some ways to mimic RI's behavior, including rehash the hash of key, and cache the hash when key/value pair is put into. I will attach patches soon.
> Test case:
> public class HashMapTest { 
> public static void main(String[] args) { 
> 	ReusableKey k = new ReusableKey(); 
> 	HashMap map = new HashMap(); 
> 	k.setKey(1); 
> 	map.put(k, "value1"); 
> 	k.setKey(18); 
> 	System.out.println(map.get(k)); 
> 	k.setKey(17); 
> 	System.out.println(map.get(k)); 
> 	} 
> } 
> class ReusableKey{ 
> 	private int key = 0; 
> 	public void setKey(int key){ 
> 		this.key = key; 
> 	} 
> 	public int hashCode(){ 
> 		return key; 
> 	} 
> 	public boolean equals(Object o){ 
> 		if(o == this){ 
> 			return true; 
> 		} 
> 		if(!(o instanceof ReusableKey)){ 
> 			return false; 
> 		} 
> 		return key == ((ReusableKey)o).key; 
> 	} 
> } 
> RI 5.0 output:
>   null 
>   null 
> Harmony output:
>   null 
>   value1 

-- 
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-206) HashMap's behavior is non-compliant with RI when key is reused

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

    Resolution: Fixed

> HashMap's behavior is non-compliant with RI when key is reused
> --------------------------------------------------------------
>
>          Key: HARMONY-206
>          URL: http://issues.apache.org/jira/browse/HARMONY-206
>      Project: Harmony
>         Type: Bug

>   Components: Classlib
>     Reporter: Paulex Yang
>     Assignee: Tim Ellison
>     Priority: Minor
>  Attachments: java.util.patch, tests.api.java.util.patch
>
> As discussion in mailing list,  when hash key is reused by modify its hashcode, Harmony's HashMap show different behavior with RI.  There are some ways to mimic RI's behavior, including rehash the hash of key, and cache the hash when key/value pair is put into. I will attach patches soon.
> Test case:
> public class HashMapTest { 
> public static void main(String[] args) { 
> 	ReusableKey k = new ReusableKey(); 
> 	HashMap map = new HashMap(); 
> 	k.setKey(1); 
> 	map.put(k, "value1"); 
> 	k.setKey(18); 
> 	System.out.println(map.get(k)); 
> 	k.setKey(17); 
> 	System.out.println(map.get(k)); 
> 	} 
> } 
> class ReusableKey{ 
> 	private int key = 0; 
> 	public void setKey(int key){ 
> 		this.key = key; 
> 	} 
> 	public int hashCode(){ 
> 		return key; 
> 	} 
> 	public boolean equals(Object o){ 
> 		if(o == this){ 
> 			return true; 
> 		} 
> 		if(!(o instanceof ReusableKey)){ 
> 			return false; 
> 		} 
> 		return key == ((ReusableKey)o).key; 
> 	} 
> } 
> RI 5.0 output:
>   null 
>   null 
> Harmony output:
>   null 
>   value1 

-- 
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-206) HashMap's behavior is non-compliant with RI when key is reused

Posted by "Paulex Yang (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/HARMONY-206?page=comments#action_12372058 ] 

Paulex Yang commented on HARMONY-206:
-------------------------------------

Your fix is better, thank you very much, Tim. And I will check HashTable, if it has same issue as your expection, I will raise another JIRA.

> HashMap's behavior is non-compliant with RI when key is reused
> --------------------------------------------------------------
>
>          Key: HARMONY-206
>          URL: http://issues.apache.org/jira/browse/HARMONY-206
>      Project: Harmony
>         Type: Bug
>   Components: Classlib
>     Reporter: Paulex Yang
>     Assignee: Tim Ellison
>     Priority: Minor
>  Attachments: java.util.patch, tests.api.java.util.patch
>
> As discussion in mailing list,  when hash key is reused by modify its hashcode, Harmony's HashMap show different behavior with RI.  There are some ways to mimic RI's behavior, including rehash the hash of key, and cache the hash when key/value pair is put into. I will attach patches soon.
> Test case:
> public class HashMapTest { 
> public static void main(String[] args) { 
> 	ReusableKey k = new ReusableKey(); 
> 	HashMap map = new HashMap(); 
> 	k.setKey(1); 
> 	map.put(k, "value1"); 
> 	k.setKey(18); 
> 	System.out.println(map.get(k)); 
> 	k.setKey(17); 
> 	System.out.println(map.get(k)); 
> 	} 
> } 
> class ReusableKey{ 
> 	private int key = 0; 
> 	public void setKey(int key){ 
> 		this.key = key; 
> 	} 
> 	public int hashCode(){ 
> 		return key; 
> 	} 
> 	public boolean equals(Object o){ 
> 		if(o == this){ 
> 			return true; 
> 		} 
> 		if(!(o instanceof ReusableKey)){ 
> 			return false; 
> 		} 
> 		return key == ((ReusableKey)o).key; 
> 	} 
> } 
> RI 5.0 output:
>   null 
>   null 
> Harmony output:
>   null 
>   value1 

-- 
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] Closed: (HARMONY-206) HashMap's behavior is non-compliant with RI when key is reused

Posted by "Tim Ellison (JIRA)" <ji...@apache.org>.
     [ http://issues.apache.org/jira/browse/HARMONY-206?page=all ]
     
Tim Ellison closed HARMONY-206:
-------------------------------


Verified by Paulex.

> HashMap's behavior is non-compliant with RI when key is reused
> --------------------------------------------------------------
>
>          Key: HARMONY-206
>          URL: http://issues.apache.org/jira/browse/HARMONY-206
>      Project: Harmony
>         Type: Bug
>   Components: Classlib
>     Reporter: Paulex Yang
>     Assignee: Tim Ellison
>     Priority: Minor
>  Attachments: java.util.patch, tests.api.java.util.patch
>
> As discussion in mailing list,  when hash key is reused by modify its hashcode, Harmony's HashMap show different behavior with RI.  There are some ways to mimic RI's behavior, including rehash the hash of key, and cache the hash when key/value pair is put into. I will attach patches soon.
> Test case:
> public class HashMapTest { 
> public static void main(String[] args) { 
> 	ReusableKey k = new ReusableKey(); 
> 	HashMap map = new HashMap(); 
> 	k.setKey(1); 
> 	map.put(k, "value1"); 
> 	k.setKey(18); 
> 	System.out.println(map.get(k)); 
> 	k.setKey(17); 
> 	System.out.println(map.get(k)); 
> 	} 
> } 
> class ReusableKey{ 
> 	private int key = 0; 
> 	public void setKey(int key){ 
> 		this.key = key; 
> 	} 
> 	public int hashCode(){ 
> 		return key; 
> 	} 
> 	public boolean equals(Object o){ 
> 		if(o == this){ 
> 			return true; 
> 		} 
> 		if(!(o instanceof ReusableKey)){ 
> 			return false; 
> 		} 
> 		return key == ((ReusableKey)o).key; 
> 	} 
> } 
> RI 5.0 output:
>   null 
>   null 
> Harmony output:
>   null 
>   value1 

-- 
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-206) HashMap's behavior is non-compliant with RI when key is reused

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

Tim Ellison reassigned HARMONY-206:
-----------------------------------

    Assign To: Tim Ellison

> HashMap's behavior is non-compliant with RI when key is reused
> --------------------------------------------------------------
>
>          Key: HARMONY-206
>          URL: http://issues.apache.org/jira/browse/HARMONY-206
>      Project: Harmony
>         Type: Bug
>   Components: Classlib
>     Reporter: Paulex Yang
>     Assignee: Tim Ellison
>     Priority: Minor
>  Attachments: java.util.patch, tests.api.java.util.patch
>
> As discussion in mailing list,  when hash key is reused by modify its hashcode, Harmony's HashMap show different behavior with RI.  There are some ways to mimic RI's behavior, including rehash the hash of key, and cache the hash when key/value pair is put into. I will attach patches soon.
> Test case:
> public class HashMapTest { 
> public static void main(String[] args) { 
> 	ReusableKey k = new ReusableKey(); 
> 	HashMap map = new HashMap(); 
> 	k.setKey(1); 
> 	map.put(k, "value1"); 
> 	k.setKey(18); 
> 	System.out.println(map.get(k)); 
> 	k.setKey(17); 
> 	System.out.println(map.get(k)); 
> 	} 
> } 
> class ReusableKey{ 
> 	private int key = 0; 
> 	public void setKey(int key){ 
> 		this.key = key; 
> 	} 
> 	public int hashCode(){ 
> 		return key; 
> 	} 
> 	public boolean equals(Object o){ 
> 		if(o == this){ 
> 			return true; 
> 		} 
> 		if(!(o instanceof ReusableKey)){ 
> 			return false; 
> 		} 
> 		return key == ((ReusableKey)o).key; 
> 	} 
> } 
> RI 5.0 output:
>   null 
>   null 
> Harmony output:
>   null 
>   value1 

-- 
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] Closed: (HARMONY-206) HashMap's behavior is non-compliant with RI when key is reused

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


Verified.

> HashMap's behavior is non-compliant with RI when key is reused
> --------------------------------------------------------------
>
>          Key: HARMONY-206
>          URL: http://issues.apache.org/jira/browse/HARMONY-206
>      Project: Harmony
>         Type: Bug

>   Components: Classlib
>     Reporter: Paulex Yang
>     Assignee: Tim Ellison
>     Priority: Minor
>  Attachments: java.util.patch, tests.api.java.util.patch
>
> As discussion in mailing list,  when hash key is reused by modify its hashcode, Harmony's HashMap show different behavior with RI.  There are some ways to mimic RI's behavior, including rehash the hash of key, and cache the hash when key/value pair is put into. I will attach patches soon.
> Test case:
> public class HashMapTest { 
> public static void main(String[] args) { 
> 	ReusableKey k = new ReusableKey(); 
> 	HashMap map = new HashMap(); 
> 	k.setKey(1); 
> 	map.put(k, "value1"); 
> 	k.setKey(18); 
> 	System.out.println(map.get(k)); 
> 	k.setKey(17); 
> 	System.out.println(map.get(k)); 
> 	} 
> } 
> class ReusableKey{ 
> 	private int key = 0; 
> 	public void setKey(int key){ 
> 		this.key = key; 
> 	} 
> 	public int hashCode(){ 
> 		return key; 
> 	} 
> 	public boolean equals(Object o){ 
> 		if(o == this){ 
> 			return true; 
> 		} 
> 		if(!(o instanceof ReusableKey)){ 
> 			return false; 
> 		} 
> 		return key == ((ReusableKey)o).key; 
> 	} 
> } 
> RI 5.0 output:
>   null 
>   null 
> Harmony output:
>   null 
>   value1 

-- 
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-206) HashMap's behavior is non-compliant with RI when key is reused

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

Paulex Yang updated HARMONY-206:
--------------------------------

    Attachment: java.util.patch
                tests.api.java.util.patch

Patches is attached to comply with RI. 

> HashMap's behavior is non-compliant with RI when key is reused
> --------------------------------------------------------------
>
>          Key: HARMONY-206
>          URL: http://issues.apache.org/jira/browse/HARMONY-206
>      Project: Harmony
>         Type: Bug
>   Components: Classlib
>     Reporter: Paulex Yang
>     Priority: Minor
>  Attachments: java.util.patch, tests.api.java.util.patch
>
> As discussion in mailing list,  when hash key is reused by modify its hashcode, Harmony's HashMap show different behavior with RI.  There are some ways to mimic RI's behavior, including rehash the hash of key, and cache the hash when key/value pair is put into. I will attach patches soon.
> Test case:
> public class HashMapTest { 
> public static void main(String[] args) { 
> 	ReusableKey k = new ReusableKey(); 
> 	HashMap map = new HashMap(); 
> 	k.setKey(1); 
> 	map.put(k, "value1"); 
> 	k.setKey(18); 
> 	System.out.println(map.get(k)); 
> 	k.setKey(17); 
> 	System.out.println(map.get(k)); 
> 	} 
> } 
> class ReusableKey{ 
> 	private int key = 0; 
> 	public void setKey(int key){ 
> 		this.key = key; 
> 	} 
> 	public int hashCode(){ 
> 		return key; 
> 	} 
> 	public boolean equals(Object o){ 
> 		if(o == this){ 
> 			return true; 
> 		} 
> 		if(!(o instanceof ReusableKey)){ 
> 			return false; 
> 		} 
> 		return key == ((ReusableKey)o).key; 
> 	} 
> } 
> RI 5.0 output:
>   null 
>   null 
> Harmony output:
>   null 
>   value1 

-- 
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-206) HashMap's behavior is non-compliant with RI when key is reused

Posted by "Paulex Yang (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/HARMONY-206?page=comments#action_12419214 ] 

Paulex Yang commented on HARMONY-206:
-------------------------------------

looks fine, thank you, Nathan.

> HashMap's behavior is non-compliant with RI when key is reused
> --------------------------------------------------------------
>
>          Key: HARMONY-206
>          URL: http://issues.apache.org/jira/browse/HARMONY-206
>      Project: Harmony
>         Type: Bug

>   Components: Classlib
>     Reporter: Paulex Yang
>     Assignee: Tim Ellison
>     Priority: Minor
>  Attachments: java.util.patch, tests.api.java.util.patch
>
> As discussion in mailing list,  when hash key is reused by modify its hashcode, Harmony's HashMap show different behavior with RI.  There are some ways to mimic RI's behavior, including rehash the hash of key, and cache the hash when key/value pair is put into. I will attach patches soon.
> Test case:
> public class HashMapTest { 
> public static void main(String[] args) { 
> 	ReusableKey k = new ReusableKey(); 
> 	HashMap map = new HashMap(); 
> 	k.setKey(1); 
> 	map.put(k, "value1"); 
> 	k.setKey(18); 
> 	System.out.println(map.get(k)); 
> 	k.setKey(17); 
> 	System.out.println(map.get(k)); 
> 	} 
> } 
> class ReusableKey{ 
> 	private int key = 0; 
> 	public void setKey(int key){ 
> 		this.key = key; 
> 	} 
> 	public int hashCode(){ 
> 		return key; 
> 	} 
> 	public boolean equals(Object o){ 
> 		if(o == this){ 
> 			return true; 
> 		} 
> 		if(!(o instanceof ReusableKey)){ 
> 			return false; 
> 		} 
> 		return key == ((ReusableKey)o).key; 
> 	} 
> } 
> RI 5.0 output:
>   null 
>   null 
> Harmony output:
>   null 
>   value1 

-- 
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