< prev index next >

src/java.base/share/classes/java/util/ArrayList.java

Print this page
rev 48215 : 8060192: Add default method <A> A[] Collection.toArray(IntFunction<A[]> generator)
Reviewed-by: martin, forax, psandoz

@@ -24,10 +24,11 @@
  */
 
 package java.util;
 
 import java.util.function.Consumer;
+import java.util.function.IntFunction;
 import java.util.function.Predicate;
 import java.util.function.UnaryOperator;
 import jdk.internal.misc.SharedSecrets;
 
 /**

@@ -415,10 +416,17 @@
         if (a.length > size)
             a[size] = null;
         return a;
     }
 
+    // no spec changes relative to supertype
+    public <T> T[] toArray(IntFunction<T[]> generator) {
+        T[] a = generator.apply(size);
+        System.arraycopy(elementData, 0, a, 0, size);
+        return a;
+    }
+
     // Positional Access Operations
 
     @SuppressWarnings("unchecked")
     E elementData(int index) {
         return (E) elementData[index];
< prev index next >