JDK-6316004 : IdentityHashMap.EntrySet.toArray(Object[]) is broken
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_9
  • CPU: sparc
  • Submitted: 2005-08-25
  • Updated: 2021-03-03
  • Resolved: 2005-08-25
Related Reports
Duplicate :  
Description
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(); // !!!!
        }

Comments
EVALUATION Sure looks like a dup of 6197726, which is currently being integrated into Mustang.
25-08-2005