United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 6312706 (coll) Map entrySet iterators should return different entries on each call to next()
6312706 : (coll) Map entrySet iterators should return different entries on each call to next()

Details
Type:
Bug
Submit Date:
2005-08-18
Status:
Closed
Updated Date:
2012-10-08
Project Name:
JDK
Resolved Date:
2011-06-16
Component:
core-libs
OS:
generic,windows_xp
Sub-Component:
java.util:collections
CPU:
x86,generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0,6,6u23
Fixed Versions:
7

Related Reports
Duplicate:
Duplicate:
Relates:
Relates:
Relates:
Relates:

Sub Tasks

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
                                     
2011-04-06
SUGGESTED FIX

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

webrev provided by Neil Richards of IBM is currently undergoing regression testing and review.
                                     
2011-03-25
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.
                                     
2011-02-02
PUBLIC COMMENTS

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

See comment of 6232484.
                                     
2005-08-18
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.
                                     
2005-08-18



Hardware and Software, Engineered to Work Together