Convert int array to List of Integer in Java
This post will discuss how to convert primitive integer array to list of Integer using plain Java, Guava library, and Apache Commons Collections.
1. Naive solution
A naive solution is to create a list of Integer and use a regular for-loop to add elements from a primitive integer array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.ArrayList; import java.util.List; class Main { // program to convert primitive integer array to list of Integer public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; List<Integer> list = new ArrayList<>(); for (int i : arr) { list.add(i); } System.out.println(list); } } |
Output:
[1, 2, 3, 4, 5]
2. Using Java 8
We can use Java 8 Stream to convert a primitive integer array to an Integer list. Following are the steps:
- Convert the specified primitive array to a sequential stream using
Arrays.stream(). - Box each element of the stream to an Integer using
IntStream.boxed(). - Use
Collectors.toList()to accumulate the input elements into a new list.
The following program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { // program to convert primitive integer array to list of Integer public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; List<Integer> list = Arrays.stream(arr) // IntStream .boxed() // Stream<Integer> .collect(Collectors.toList()); System.out.println(list); } } |
Output:
[1, 2, 3, 4, 5]
We can also use IntStream.of() to get IntStream from integer array.
|
1 2 3 4 |
// using `IntStream.of()` List<Integer> list = IntStream.of(arr) // returns IntStream .boxed() .collect(Collectors.toList()); |
Here’s another version that uses Stream. First, we convert a primitive integer array to Integer array and then use Collections.addAll() to add all elements of the Integer array to the list of Integer, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 |
int[] arr = { 1, 2, 3, 4, 5 }; // Converting primitive integer array to an Integer array Integer[] boxedArray = Arrays.stream(arr).boxed().toArray(Integer[]::new); // add all elements of the Integer array to a list of Integer List<Integer> list = new ArrayList<>(); Collections.addAll(list, boxedArray); // print the list System.out.println(list); |
3. Using Guava Library
We can also use Guava’s Ints.asList() to get a fixed-size list view of the specified array, similar to Arrays.asList(). It will return List<Integer> unlike Arrays.asList(), which returns List<int[]>.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import com.google.common.primitives.Ints; import java.util.List; class Main { // program to convert primitive integer array to list of Integer public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; List<Integer> list = Ints.asList(arr); System.out.println(list); } } |
Output:
[1, 2, 3, 4, 5]
Since an array cannot be structurally modified, if we try to add or remove elements from the list, an UnsupportedOperationException will be thrown. If we want a mutable list, we can use:
|
1 |
List<Integer> list = Lists.newArrayList(Ints.asList(arr)); |
4. Using Apache Commons Lang
Arrays.asList(int[]) returns a List<int[]> not List<Integer>.
|
1 2 3 |
int[] arr = { 1, 2, 3, 4, 5 }; List<int[]> list = Arrays.asList(arr); System.out.println(Arrays.toString(list.get(0))); // [1, 2, 3, 4, 5] |
To get List<Integer>, we need to convert an array of primitive ints to the Integer array first. We can use the ArrayUtils.toObject() method provided by Apache Commons lang for conversion, as shown below:
|
1 |
List<Integer> list = Arrays.asList(ArrayUtils.toObject(arr)); |
That’s all about converting int array to Integer List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)