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

IdentityHashMap.entrySet().toArray() doesn't work

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.4.0
    • core-libs
    • None
    • beta2
    • generic
    • generic
    • Verified

        Due to internal details of the Iterator returned by IdentityHashMap.entrySet().iterator(), the toArray method in question will contain size() identical object references to a non-functional Map.Entry. Any attempt to use the Entry will throw an Index out of bounds error. The following program should generate no output and exit normally:

      import java.util.*;

      public class ToArray {
          public static void main(String[] args) {
              Map m = new IdentityHashMap();
              m.put("french", "connection");
              m.put("polish", "sausage");
              Object[] mArray = m.entrySet().toArray();
              if (mArray[0] == mArray[1])
                  System.out.println("Broken");

              // Calls toString on mArray[0], which throws IndexOutOfBoundsException
              mArray[0].toString();
          }
      }

      Instead it prints "broken" and throws an IndexOutOfBoundsException.

            jjb Josh Bloch
            jjb Josh Bloch
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: