JDK-6312706 : (coll) Map entrySet iterators should return different entries on each call to next()
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 5.0,6,6u23
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,windows_xp
  • CPU: generic,x86
  • Submitted: 2005-08-18
  • Updated: 2012-10-08
  • Resolved: 2011-06-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b140Fixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
Some Map implementations have an entrySet() method returning a set
with an iterator method returning an iterator which returns the
same (as in object identity) Map.Entry on each call to next().
This works well in the usual case where each entry is discarded
(effectively becomes garbage) before next() is called.
However, if entries are saved, perhaps to build up other collections or
arrays, they will need to be defensively copied by the caller before next()
is called.  This is an optimization to prevent garbage being generated on each
call to next().  However, the cost of creating many small short-lived objects is much 
lower than it used to be in most implementations, and is likely to get even lower as VM
optimizations such as escape analysis and stack allocation are implemented.
Therefore such error-prone optimizations such as this one should be removed at
some point.

Comments
EVALUATION Committed as http://hg.openjdk.java.net/jdk7/tl/jdk/rev/c1e87a18e46a
06-04-2011

SUGGESTED FIX http://cr.openjdk.java.net/~mduigou/6312706/0/webrev/
26-03-2011

SUGGESTED FIX webrev provided by Neil Richards of IBM is currently undergoing regression testing and review.
25-03-2011

PUBLIC COMMENTS Re-opened for the following case: Map map = new HashMap(); map.put("Alpha", "1"); map.put("Bravo", "2"); Set<Map.Entry> set = new HashSet<>(map.entrySet()); assert set.equals(map.entrySet()) : "Sets should be equal"; EnumMap and ConcurrentHashMap are known to suffer from this problem.
02-02-2011

PUBLIC COMMENTS Annoying yes, but allowed by the spec. No current plans to fix.
24-12-2010

EVALUATION The obvious fix of returning a new Map.Entry from each call to next() should be implemented, some microbenchmarks run, and the decision as to whether to switch in mustang or dolphin should be made on that basis.
18-08-2005

SUGGESTED FIX See comment of 6232484.
18-08-2005