Collection.toArray(IntFunction) in Java 11
Java 11 added a default method toArray(IntFunction<T[]> generator) to the Collection interface. It converts any collection into a correctly typed array with a constructor reference: list.toArray(String[]::new).
This tutorial compares the three toArray variants and shows where typed arrays are still needed.
1. Why Was This Method Added?
toArray() returns Object[], which loses the element type. toArray(T[] a) keeps the type but needs an array argument, and developers often argued about passing new String[0] or new String[size]. The generator form says clearly "give me a String[]" and matches Stream.toArray(String[]::new) from Java 8.
2. The Three toArray Variants
| Signature | Since | Result |
|---|---|---|
Object[] toArray() | 1.2 | untyped Object[] |
<T> T[] toArray(T[] a) | 1.2 | typed array; reuses a if it is big enough |
<T> T[] toArray(IntFunction<T[]> generator) | 11 | typed array created by the generator |
The default implementation calls generator.apply(0) and passes the result to toArray(T[]), so it behaves like toArray(new T[0]).
3. Practical Examples
Example 1: Comparing the variants
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class ToArrayGenerator {
public static void main(String[] args) {
List<String> fruits = List.of("mango", "apple", "guava");
// Before: pass an array (often a zero-length one)
String[] a1 = fruits.toArray(new String[0]);
// Java 11: pass a generator, usually a constructor reference
String[] a2 = fruits.toArray(String[]::new);
System.out.println("toArray(new String[0]) : " + Arrays.toString(a1));
System.out.println("toArray(String[]::new) : " + Arrays.toString(a2));
System.out.println("Element type : " + a2.getClass().getComponentType().getSimpleName());
// Works for every Collection, e.g. a sorted Set
Set<Integer> marks = new TreeSet<>(List.of(72, 95, 64));
Integer[] sorted = marks.toArray(Integer[]::new);
System.out.println("From TreeSet : " + Arrays.toString(sorted));
// Object[] toArray() still exists but loses the element type
Object[] plain = fruits.toArray();
System.out.println("toArray() type : " + plain.getClass().getSimpleName());
}
}
toArray(new String[0]) : [mango, apple, guava] toArray(String[]::new) : [mango, apple, guava] Element type : String From TreeSet : [64, 72, 95] toArray() type : Object[]
Example 2: Passing a list to an array or varargs API
Many APIs, such as Paths.get(String, String...) and String.join, accept arrays or varargs.
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class VarargsBridge {
public static void main(String[] args) {
// Many older APIs take arrays or varargs; toArray(String[]::new) bridges a List to them
List<String> parts = new ArrayList<>(List.of("reports", "2026", "q3"));
Path report = Paths.get("data", parts.toArray(String[]::new));
System.out.println(report);
String joined = String.join(" / ", parts.toArray(String[]::new));
System.out.println(joined);
}
}
data/reports/2026/q3 reports / 2026 / q3
4. How the Generator Is Used
5. Important Points and Best Practices
- Prefer
toArray(String[]::new)for readability; it is as fast astoArray(new String[0]). - Avoid passing a pre-sized array: it saves nothing on modern JVMs and can leave a stale
nullif the collection shrinks concurrently. - Primitive arrays need streams instead:
list.stream().mapToInt(Integer::intValue).toArray(). - If you do not need an array, keep working with the collection.
6. Summary
- Java 11 added
Collection.toArray(IntFunction<T[]>). - Use it with a constructor reference such as
String[]::new. - It returns a typed array, unlike
toArray(), which returnsObject[]. - It is the clearest way to pass collection contents to array or varargs APIs.
Related Tutorials
7. References
Frequently Asked Questions (FAQ)
Q1. How do I convert a List to an array in Java 11?
Use list.toArray(String[]::new) (with the element type you need). It returns a correctly typed array.
Q2. Why does toArray() return Object[]?
Because of type erasure, a collection does not know its element type at run time, so the no-argument version can only create an Object[].
Q3. Is toArray(new String[0]) slower than toArray(new String[size])?
No. On modern JVMs the zero-length version is as fast or faster, and the generator form behaves the same way.
Q4. Can toArray create a primitive array like int[]?
No. Use a stream, for example list.stream().mapToInt(Integer::intValue).toArray().