-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.3.1
-
x86
-
windows_2000
Name: ddT132432 Date: 07/24/2001
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
WeakHashMap's entry set's iterator is incorrectly implemented, since in each
call to hasNext() it advances the iterator. In the following code, two
iterators are created, only in the second case hasNext() is called three times
first. The entry returned eventually is not the same as in the original case
when next() is called immediately after iterator is created.
import java.util.*;
public class Test {
public static void main(String[] args) {
Map map=new WeakHashMap();
map.put("k1","v1");
map.put("k2","v2");
map.put("k3","v3");
Set entrySet=map.entrySet();
Iterator iterator=entrySet.iterator();
Map.Entry e=(Map.Entry)iterator.next();
print(e);
iterator=entrySet.iterator();
System.out.println(iterator.hasNext()); // silently advances!
System.out.println(iterator.hasNext());
System.out.println(iterator.hasNext());
Map.Entry e1=(Map.Entry)iterator.next();
print(e1); // e1 will not contain samve values as e!
}
protected static void print(Map.Entry e) {
System.out.println("("+e.getKey()+", "+e.getValue()+")");
}
}
(Review ID: 128693)
======================================================================
- duplicates
-
JDK-4236533 WeakHashMap.entrySet().iterator() behaves incorrectly
-
- Closed
-