How to Convert a List to an Array in Java
Convert a Java List into an array using toArray and stream-based approaches.
How to Convert a List to an Array in Java
A List and an array hold the same elements but expose different APIs: a List resizes and carries rich methods, while an array is fixed-length and is what many older or low-level APIs expect. Converting from one to the other is a one-line job in modern Java — the only real choice is which form reads best and whether you need an object array or a primitive one.
The idiomatic way: toArray with a typed array
List.toArray(T[]) returns a strongly typed array. Pass a zero-length array of the element type and let the JDK size the result for you:
List<String> names = List.of("Ann", "Bob", "Cy");
String[] arr = names.toArray(new String[0]);The new String[0] argument carries the type (String[]), not a pre-sized buffer. On modern JVMs the empty-array form is the recommended one — it is as fast as a perfectly sized array and avoids the bug where a too-small array gets reallocated and a too-large one leaves trailing nulls. Reach for this whenever you need an Object-type array such as String[], Integer[], or your own class.
The Java 11+ form: an array constructor reference
Since Java 11 you can pass an array constructor reference instead of a literal empty array. It says exactly what it means — "make me a String[]":
String[] arr = names.toArray(String[]::new);This compiles to the same thing as new String[0] but reads more clearly. Note the no-argument toArray() is a trap: it always returns Object[], never String[], so casting its result to String[] throws ClassCastException at runtime.
| Approach | Result type | Notes |
|---|---|---|
list.toArray(new String[0]) | String[] | Recommended for object arrays |
list.toArray(String[]::new) | String[] | Java 11+, clearest form |
list.toArray() | Object[] | Loses the element type; rarely what you want |
list.stream().mapToInt(...).toArray() | int[] | The only way to reach a primitive array |
Primitive arrays go through a stream
toArray can only produce arrays of objects. A List<Integer> cannot become an int[] directly — autoboxing does not extend to arrays. Use a stream to unbox each element:
List<Integer> nums = List.of(10, 20, 30);
int[] prim = nums.stream().mapToInt(Integer::intValue).toArray();The same pattern gives you long[] (mapToLong) and double[] (mapToDouble). There is no JDK shortcut for primitive arrays, so the stream is the idiomatic route.
A worked example
This program runs every approach side by side, prints the runtime type the no-arg toArray actually returns, and proves the resulting array is an independent copy — editing it does not touch the original list.
What to take from the run:
toArray(new String[0])andtoArray(String[]::new)both print[Ann, Bob, Cy]— they are two spellings of the same typed conversion, and you can use whichever reads better in your codebase.- The no-arg
toArray()reports its runtime type asObject[], notString[]— concrete proof that it erases the element type and why you should avoid casting its result. - The
List<Integer>becomes a realint[]only aftermapToInt; the printed[10, 20, 30]is a primitive array, not anInteger[], so no boxing survives. Arrays.stream(prim).sum()prints60, confirming the result is a usable primitive array you can feed straight into numeric stream operations.- After
a1[0] = "ZZ"the array prints[ZZ, Bob, Cy]while the list still prints[Ann, Bob, Cy]—toArrayreturns an independent copy, so changes to the array never leak back into the source list.
Practice
You have a List<Integer> and need an int[]. Which expression produces it correctly?