Java
Programming
Data Conversion
Arrays
List<Integer>

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

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Converting an array of primitive int values to a List<Integer> is a common requirement in Java programming, especially when dealing with libraries or APIs that prefer collection types over arrays. Since Java's collections framework does not handle primitive types directly, primitives must be converted to their corresponding wrapper classes; in this case, int must be converted to Integer. This conversion involves a few different methods, some of which are more suitable depending on your particular use case and Java version.

Using Arrays.asList() and Stream in Java 8 and Above

Java 8 introduced Streams, which provide a more declarative approach to handle collections and arrays. Here’s how you can convert an int[] to List<Integer> using Java 8 Streams:

java
1import java.util.Arrays;
2import java.util.List;
3import java.util.stream.Collectors;
4
5public class ArrayToList {
6    public static void main(String[] args) {
7        int[] array = {1, 2, 3, 4, 5};
8        List<Integer> list = Arrays.stream(array)    // Convert int[] to IntStream
9                                   .boxed()          // Box int values to Integer
10                                   .collect(Collectors.toList()); // Collect as List
11        System.out.println(list);
12    }
13}

Using Traditional Loop

For earlier versions of Java or for scenarios where you want to avoid Streams for simplicity or performance reasons, you can use a simple loop to convert an array to a list:

java
1import java.util.ArrayList;
2import java.util.List;
3
4public class ArrayToList {
5    public static void main(String[] args) {
6        int[] array = {1, 2, 3, 4, 5};
7        List<Integer> list = new ArrayList<>();
8        for (int value : array) {
9            list.add(value); // Autoboxing converts int to Integer automatically
10        }
11        System.out.println(list);
12    }
13}

Using Arrays.setAll() Method

Another method that is less commonly used but provides an alternative way to achieve the conversion, especially when you need to apply a transformation to the values, is using Arrays.setAll():

java
1import java.util.Arrays;
2import java.util.List;
3import java.util.stream.Collectors;
4import java.util.stream.IntStream;
5
6public class ArrayToList {
7    public static void main(String[] args) {
8        int[] array = {1, 2, 3, 4, 5};
9        Integer[] integerArray = new Integer[array.length];
10        Arrays.setAll(integerArray, i -> array[i]); // Set Integer[] based on int[] values
11        List<Integer> list = Arrays.asList(integerArray);
12        System.out.println(list);
13    }
14}

Performance Considerations

Using streams might be less efficient compared to using a traditional for-loop for large arrays or in performance-critical sections of the code due to the overhead of boxing operations and the creation of stream objects. In contrast, the traditional loop is straightforward and might be optimized by the JIT compiler in the JVM.

Practical Example Using Apache Commons Lang

If you are already using Apache Commons Lang in your project, you can leverage ArrayUtils to simplify your task:

java
1import org.apache.commons.lang3.ArrayUtils;
2import java.util.List;
3import java.util.Arrays;
4
5public class ArrayToList {
6    public static void main(String[] args) {
7        int[] array = {1, 2, 3, 4, 5};
8        Integer[] integerArray = ArrayUtils.toObject(array);
9        List<Integer> list = Arrays.asList(integerArray);
10        System.out.println(list);
11    }
12}

Summary Table

MethodDescriptionJava Version
Streams (Arrays.stream())Uses Streams API for a declarative approach. Includes boxing and is ideal for Java 8+.8+
Traditional For-LoopUses explicit looping and autoboxing. Preferred for its simplicity and control.1.2+
Arrays.setAll()Useful for simultaneously transforming values. Less common.8+
Apache Commons Lang (ArrayUtils)Simplifies conversion if external libraries are acceptable.External

Converting primitive arrays to object collections is a frequent requirement, especially when working with modern Java APIs that favor collection-based approaches. The above methods provide a variety of ways to perform these conversions, each with its trade-offs in terms of readability, performance, and external dependencies. Choose the method that best suits your specific scenario.


Related reading
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
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms