Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4483818

WeakHashMap's entrySet().iterator() is incorrectly implemented

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.1
    • core-libs



      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)
      ======================================================================

            jjb Josh Bloch (Inactive)
            ddressersunw Daniel Dresser (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: