Array to List 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.List;
import java.util.ArrayList;
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 list = new ArrayList[arr.length];
for [int i: arr] {
list.add[Integer.valueOf[i]];
}
System.out.println[list];
}
}

DownloadRun Code

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:

  1. Convert the specified primitive array to a sequential stream using Arrays.stream[].
  2. Box each element of the stream to an Integer using IntStream.boxed[].
  3. 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 list = Arrays.stream[arr]// IntStream
.boxed[]// Stream
.collect[Collectors.toList[]];
System.out.println[list];
}
}

DownloadRun Code

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 list = IntStream.of[arr]// returns IntStream
.boxed[]
.collect[Collectors.toList[]];


Heres 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 list = new ArrayList[];
Collections.addAll[list, boxedArray];
// print the list
System.out.println[list];

DownloadRun Code

3. Using Guava Library

We can also use Guavas Ints.asList[] to get a fixed-size list view of the specified array, similar to Arrays.asList[]. It will return List unlike Arrays.asList[], which returns List.

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 list = Ints.asList[arr];
System.out.println[list];
}
}

Download Code

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 list = Lists.newArrayList[Ints.asList[arr]];

4. Using Apache Commons Lang

Arrays.asList[int[]] returns a List not List.

1
2
3
int[] arr = { 1, 2, 3, 4, 5 };
List list = Arrays.asList[arr];
System.out.println[Arrays.toString[list.get[0]]];// [1, 2, 3, 4, 5]


To get List, 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 list = Arrays.asList[ArrayUtils.toObject[arr]];

Thats all about converting int array to Integer List in Java.

Video liên quan

Chủ Đề