00:00

Collection.toArray(IntFunction) in Java 11

Last updated: · Standard (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

SignatureSinceResult
Object[] toArray()1.2untyped Object[]
<T> T[] toArray(T[] a)1.2typed array; reuses a if it is big enough
<T> T[] toArray(IntFunction<T[]> generator)11typed 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

ToArrayGenerator.java
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());
    }
}
Output
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.

VarargsBridge.java
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);
    }
}
Output
data/reports/2026/q3
reports / 2026 / q3

4. How the Generator Is Used

toArray with a generator The collection calls the generator with zero to get an empty typed array, then uses toArray(T[]) to return a new array of the right size filled with the elements. List<String> 3 elements generator.apply(0) new String[0] toArray(T[]) allocates String[3] String[] [mango, apple, guava]
Figure 1: The generator only supplies the array type; the collection decides the size.

5. Important Points and Best Practices

  • Prefer toArray(String[]::new) for readability; it is as fast as toArray(new String[0]).
  • Avoid passing a pre-sized array: it saves nothing on modern JVMs and can leave a stale null if 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 returns Object[].
  • It is the clearest way to pass collection contents to array or varargs APIs.

Related Tutorials

7. References