aspose file tools*
The moose likes Java in General and the fly likes A Map.Entry object not becoming invalid after map modification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "A Map.Entry object not becoming invalid after map modification" Watch "A Map.Entry object not becoming invalid after map modification" New topic
Author

A Map.Entry object not becoming invalid after map modification

Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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?

SCJP2. Please Indent your code using UBB Code
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
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).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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(label);
System.out.println(" map: " + map);
System.out.println(" entrySet: " + entrySet);
System.out.println(" entry: " + entry);
System.out.println();
}
}
</pre>


"I'm not back." - Bill Harding, Twister
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
thank you both
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A Map.Entry object not becoming invalid after map modification