How to convert int[] into List<Integer> in Java?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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:
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:
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():
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:
Summary Table
| Method | Description | Java Version |
Streams (Arrays.stream()) | Uses Streams API for a declarative approach. Includes boxing and is ideal for Java 8+. | 8+ |
| Traditional For-Loop | Uses 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.

