JDK-5031890 : (coll spec) Collection toArray() does not specify empty collection behavior
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-04-15
  • Updated: 2012-10-08
  • Resolved: 2005-08-06
Related Reports
Relates :  
Description
Name: rmT116609			Date: 04/14/2004


A DESCRIPTION OF THE PROBLEM :
The API Javadoc for Collection toArray() does not specify empty collection behavior. Since the List API Javadoc references the Collection Javadoc, this should be mentioned.

The Javadoc states that it "Returns an array containing all of the elements in this collection." However, the observed behavior seems that if a Collection (or, in my case, a List) is empty, it returns null.

I'd hope the Javadoc should mention null, empty array, or explicitly undefined

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I'd hope the Javadoc should mention null, empty array, or explicitly undefined behavior for an empty collection or list
ACTUAL -
The Javadoc states that it "Returns an array containing all of the elements in this collection." However, the observed behavior seems that if a Collection (or, in my case, a List) is empty, it returns null.

URL OF FAULTY DOCUMENTATION :
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collection.html#toArray()
(Incident Review ID: 250281) 
======================================================================

Comments
EVALUATION I tried to reproduce this in 1.4 and later, but failed. Here's my test program (which should be turned into a real regression test): -------------------------------------------------------------- import java.io.*; import java.util.*; public class Bug { static int passed = 0, failed = 0; static void fail(String msg) { failed++; new Exception(msg).printStackTrace(); } static void unexpected(Throwable t) { failed++; t.printStackTrace(); } static void check(boolean condition, String msg) { if (condition) passed++; else fail(msg); } static void check(boolean condition, String msg, Object o) { if (condition) passed++; else fail(msg + ": "+ o.getClass()); } static void check(boolean condition) { check(condition, "Something's wrong"); } static void fail(String msg, Object o) { fail(msg + ": class=" + o.getClass()); } private static void checkEmptyCollection(Collection c) throws Throwable { check(c.toArray().length == 0, "non-empty array", c); check(c.toArray(new Object[0]).length == 0, "non-empty array", c); Object[] a = new Object[1]; a[0] = Boolean.TRUE; check(c.toArray(a) == a && a[0] == null, "bad array", c); } private static void checkEmptyMap(Map m) throws Throwable { checkEmptyCollection(m.keySet()); checkEmptyCollection(m.values()); checkEmptyCollection(m.entrySet()); } public static void main(String[] args) throws Throwable { checkEmptyCollection(new ArrayList()); checkEmptyCollection(new Vector()); checkEmptyCollection(new LinkedList()); checkEmptyCollection(new HashSet()); checkEmptyCollection(new LinkedHashSet()); checkEmptyCollection(new TreeSet()); checkEmptyCollection(Collections.EMPTY_SET); //checkEmptyCollection(new PriorityQueue()); checkEmptyMap(new HashMap()); checkEmptyMap(new LinkedHashMap()); checkEmptyMap(new WeakHashMap()); checkEmptyMap(new IdentityHashMap()); checkEmptyMap(new Hashtable()); checkEmptyMap(Map.EMPTY_MAP); System.out.println("passed = "+passed+", failed = "+failed+"\n"); if (failed > 0) throw new Exception("Some tests failed"); } } -------------------------------------------------------------- This passes perfectly, except for one failure due to running into 6197726 (coll) IdentityHashMap.EntrySet.toArray(T[] a) is incorrectly implemented
06-08-2005