• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

A Map.Entry object not becoming invalid after map modification  RSS feed

Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API for Map.Entry states:
"These Map.Entry objects are valid only for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator."
But I haven't been able to reproduce any invalid behaviour in this program:

As you can see the pair is perfectly funtional after a modification of the map.
Yes the iterator is fail-fast.
I also tried with the remove method in the commented line and with a HasMap and the results were identical.
Is the API guarging against future implementations or the invalidation of a Map.Entry object requires more hard work to be seen?
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something is undefined, which means it can do whatever, but don't count on it. Don't count that it does something wrong nor correct.
This is a very important concepts:
"Never use any undocumented feature, even you like it."
It will be back to bite you at any time, and you cannot complain on it, since it told you so.

SCJD Study Group has been moved to http://www.developergroup.org/
[This message has been edited by Roseanne Zhang (edited September 21, 2001).]
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API never says what exactly should happen to the Map.Entry if the underlying Map is altered. So just about any behavior is OK as far as the API is concerned. The fact that the Map.Entry is no longer "valid" doesn't mean that it's impossible to use - it means that you can't trust the results you get. Different implementations of Map may do different things. To see this run the following program, and see how HashMap and TreeMap react differently.
<pre>import java.util.*;

public class TestMapEntry2 {

public static void main(String[] args)
test(new TreeMap());
test(new HashMap());

static void test(Map map)
for(char c ='a'; c < 'f';c++)
map.put(new Integer((int) c), new Character(c));
Set entrySet = map.entrySet();
Iterator it = entrySet.iterator();
Map.Entry entry = (Map.Entry) it.next();
show("A", map, entrySet, entry);
map.put(new Integer((int) 'z'), new Character('z'));
show("B", map, entrySet, entry);
map.remove(new Integer(101));
show("C", map, entrySet, entry);
map.put(new Integer(97), new Character('z'));
show("D", map, entrySet, entry);
map.remove(new Integer(97));
show("E", map, entrySet, entry);
map.put(new Integer(97), new Character('q'));
show("F", map, entrySet, entry);
entry.setValue(new Character('x'));
show("G", map, entrySet, entry);
try { entry = (Map.Entry) it.next(); }
catch (Exception e) { e.printStackTrace(System.out); }

static void show(String label, Map map, Set entrySet, Map.Entry entry)
System.out.println(" map: " + map);
System.out.println(" entrySet: " + entrySet);
System.out.println(" entry: " + entry);
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you both
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!