Java
Programming
Data Conversion
Integer Array
Collection Framework

How to convert List<Integer> to int[] in Java

Codemia Knowledge Hub ·

To turn a List<Integer> into an int[] on Java 8 and later, the shortest way is list.stream().mapToInt(Integer::intValue).toArray(). A plain for loop works on any Java version, and if you already depend on Apache Commons Lang, ArrayUtils.toPrimitive does it in one call.

Stream API (Java 8+)

The Stream API converts a List<Integer> to an int[] in one concise expression:

java
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int[] array = list.stream().mapToInt(Integer::intValue).toArray();
  1. list.stream() creates a stream from the list.
  2. .mapToInt(Integer::intValue) turns the stream of Integer objects into an IntStream of primitive int values.
  3. .toArray() collects the stream into a new int[].

Plain for loop

Use this before Java 8, or when you want the least overhead:

java
1List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
2int[] array = new int[list.size()];
3for (int i = 0; i < list.size(); i++) {
4    array[i] = list.get(i);
5}

It iterates over the list and copies each value into the matching index of a new array. It is simple, just more verbose than the stream version.

Apache Commons Lang ArrayUtils

java
import org.apache.commons.lang3.ArrayUtils;
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int[] array = ArrayUtils.toPrimitive(list.toArray(new Integer[0]));
  1. list.toArray(new Integer[0]) converts the List<Integer> to an Integer[].
  2. ArrayUtils.toPrimitive() converts the Integer[] to an int[].

It is concise and readable, but it adds a dependency, so it makes most sense when the project already includes Commons Lang.

Which is fastest

Performance depends on list size and the environment. The for loop may be slightly faster than the Stream API because it has less overhead, and the Commons Lang version can be slower because it converts through an intermediate array.

MethodDescriptionPerformanceExternal Libraries Needed?
Stream APIUses Java 8's Stream API to convert lists to arrays via streams.Fast for small-medium listsNo
Traditional For-LoopManually iterates and copies elements from the list to the array.Very FastNo
Apache Commons LangUtilizes Apache Commons Lang’s ArrayUtils for conversion.Medium due to additional conversionsYes

Pick based on your Java version, performance needs and existing dependencies: streams for concise modern code, the loop for control and speed, Commons Lang when it is already on the classpath.


Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms