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

IdentityHashMap.EntrySet.toArray(Object[]) is broken

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 5.0
    • core-libs
    • None

      IdentityHashMap.EntrySet.toArray(Object[]) doesn't return a value which follows the type of the array which is passed in.

      import java.util.*;

      public class ihashmap {
          public static void main(String[] args) {
              IdentityHashMap hm = new IdentityHashMap();
              Set set = hm.entrySet();
              String[] array = (String[]) set.toArray(new String[0]);
          }
      }

      Compile this with 1.4.2. Running with 1.4.2 succeeds but 1.5.0 fails with

      Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
      at ihashmap.main(ihashmap.java:7)

      The code explains it all.

              public <T> T[] toArray(T[] a) {
      return (T[])toArray(); // !!!!
              }

            martin Martin Buchholz
            never Tom Rodriguez
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: