United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6232484 : (coll) ArrayList made from IdentityHashMap.entrySet() fails to create properly

Details
Type:
Bug
Submit Date:
2005-02-24
Status:
Closed
Updated Date:
2012-10-08
Project Name:
JDK
Resolved Date:
2005-09-04
Component:
core-libs
OS:
windows_2000
Sub-Component:
java.util:collections
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:

Related Reports
Relates:
Relates:
Relates:

Sub Tasks

Description
FULL PRODUCT VERSION :
compiled on windows with java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

or java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)


run on Windows with java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

run on linux with java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)



ADDITIONAL OS VERSION INFORMATION :
Windows 2000 SP4 (Microsoft Windows 2000 [Version 5.00.2195]),
Linux (Linux 2.6.4 #1 SMP  i686  GNU/Linux)



A DESCRIPTION OF THE PROBLEM :
When creating a new ArrayList passing it a result of entrySet() called on IdentityHashMap of size one, the resulting ArrayList is of size one but contains null instead of a reference to object the original IdentityHashMap contains.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the sample testcase provided.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
ls: [com.parsek.test.BugTest$KeyClass@35ce36=com.parsek.test.BugTest$ValueClass@1a46e30]
ls2: [com.parsek.test.BugTest$KeyClass@35ce36=com.parsek.test.BugTest$ValueClass@1a46e30]
ACTUAL -
ls: [null]
ls2: [com.parsek.test.BugTest$KeyClass@35ce36=com.parsek.test.BugTest$ValueClass@1a46e30]

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.*;

public class BugTest {

	static class KeyClass {
		String somefield;
	}
	
	static class ValueClass {
		String somevalue;
	}

	public static void main(String [] args) throws Exception {
	
		IdentityHashMap map = new IdentityHashMap();
		map.put(new KeyClass(), new ValueClass());

		ArrayList ls = new ArrayList(map.entrySet());
		System.out.println("ls: " + ls);
		
		LinkedList ls2 = new LinkedList(map.entrySet());
		System.out.println("ls2: " + ls2);

	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Use LinkedList or HashSet instead of ArrayList
###@###.### 2005-2-24 02:21:22 GMT

                                    

Comments
EVALUATION

Has the same root cause as

6197726: (coll) IdentityHashMap.entrySet().toArray(T[] a) is incorrectly implemented

The deeper problem is that IdentityHashMap.iterator() wants to return the *same* entry
(with different keys and values) for every call to next(), which is a very bad idea.

Hmmmmm...found the contributed fix just now... I was thinking on exactly the same lines...
The proposed fix for 6197726 appears to make the submitted test work again, but
the deeper problem remains.  A more comprehensive fix on the lines of the contributed
fix is needed.

BTW, here's a simpler test case:

--------------------------------------------------------------
import java.util.*;

public class Bug {

    public static void main(String [] args) throws Exception {

	Map<String,String> map = new IdentityHashMap<String,String>();
	map.put("foo", "bar");
	map.put("baz", "quux");

	ArrayList<Map.Entry<String,String>> ls
	    = new ArrayList<Map.Entry<String,String>>(map.entrySet());
	System.out.println("ls: " + ls);

	LinkedList<Map.Entry<String,String>> ls2
	    = new LinkedList<Map.Entry<String,String>>(map.entrySet());
	System.out.println("ls2: " + ls2);
    }
}
--------------------------------------------------------------
                                     
2005-08-10
EVALUATION

The deeper problem of returning the same object on each call to next()
is being addressed via 6312706.  Most of the obvious manifestations
of that behavior are being fixed here.
                                     
2005-08-18



Hardware and Software, Engineered to Work Together