Convert a Primitive Array to List in Java

Learn to convert an array of primitives (int, long, float, double, boolean) to a List in Java using the iteration, Streams and libraries such as Guava and Apache Commons Lang3.

1. Autoboxing Works with a Single Primitive Value, Not with Array of Primitives

In Java, we can store primitive values and objects in an array. Converting from an array of objects to a List is straightforward using the method Arrays.asList(arrayOfObjects). But the same technique does not work with the array of primitive values because Java autoboxing feature works with single primitive values, but not with array types.

List<Integer> list = Arrays.asList(new Integer[]{1,2,3,4});	//Works!

List<Integer> list = Arrays.asList(new int[]{1,2,3,4});	//Does not work!

2. Using Core Java

The following examples use the core Java APIs for converting the primitive array to List and do not require extra dependencies in the project.

2.1. Using Streams

In Java, we can represent the stream of primitives using the IntStream, LongStream, FloatStream, DoubleStream classes for corresponding primitive types. They all contain boxed() method, which converts a primitive to its wrapper object type. For example, int will be converted to Integer type.

Finally, we collect the boxed object types to a List using the Stream.toList() method.

int[] intArray = new int[]{0, 1, 2, 3, 4, 5};

List<Integer> list = IntStream.of(intArray).boxed().toList();

2.2. Using Iteration

Rather than using the Stream API, we can also use the standard iteration approach to box each element in the array and store it in the List.

int[] intArray = new int[]{0, 1, 2, 3, 4, 5};

List<Integer> list = new ArrayList<Integer>();
for (int e : intArray) {
  list.add(e);
}

3. Using Open-source Libraries

For a more readable code, we can opt for these solutions provided by open-source libraries.

3.1. Guava’s Ints.asList()

The Ints.asList() takes the primitive array and returns a List of object types.

int[] intArray = new int[]{0, 1, 2, 3, 4, 5};

List<Integer> list = Ints.asList(intArray);

Do not forget to include the latest version of Guava library in the project’s classpath.

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.1-jre</version>
</dependency>

3.2. Commons Lang3’s ArrayUtils.toObject()

The ArrayUtils.toObject() converts an array of primitive values to array of object types. Then we can collect the array of objects to List using the standard Arrays.asList() method.

int[] intArray = new int[]{0, 1, 2, 3, 4, 5};

List<Integer> list = Arrays.asList(ArrayUtils.toObject(intArray));

Do not forget to include the latest version of commons-lang3 library in the project’s classpath.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

4. Conclusion

In this short Java tutorial, we learned to convert an array of primitive values to a List using the core Java APIs and solutions offered by other libraries. We need these solutions because Java does not support autoboxing of array types, and autoboxing of only a single primitive value is allowed.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode