In this post, we will learn how to convert arrays of primitive type int to lists of wrapper type Integer in Java using the Guava Ints.asList() method. We will also see the advantages and disadvantages of using this method, and how it compares with other ways to achieve the same in Java.

1. Limitations of Arrays.asList() method

You might have used the built-in Arrays.asList() method, which takes an array of objects as an argument and returns a list of objects. For example:

Download Code

 
However, this method does not work with arrays of primitive types. If you pass an array of primitive type int as an argument, it will return a list of int[], not a list of Integer. For example:

Download Code

2. Overview of Ints.asList() method

You can use Guava Ints.asList() method to convert arrays of primitive type int to lists of wrapper type Integer in Java. It takes an array of primitive type int as an argument and returns a fixed-size list of wrapper type Integer backed by the specified array. The syntax is as follows:

 
Here, backingArray is the array to back the list. The method infers the generic type parameter from the argument, so you don’t have to specify it explicitly. For example:

Download Code

3. Advantages and Disadvantages of Ints.asList() method

Some of the advantages of using the Guava Ints.asList() method are:

  • It works with arrays of primitive types. Unlike the built-in Arrays.asList() method, it can take an array of primitive type int as an argument and return a list of wrapper type Integer. This can help you avoid unnecessary boxing and unboxing overheads or creating intermediate arrays.
  • It reduces boilerplate code and improves readability. You don’t have to specify the generic type parameter explicitly, which can be verbose and redundant.

Some of the disadvantages of using the Guava Ints.asList() method are:

  • Like Arrays.asList(), it returns a fixed-size list backed by the original array. This means that you cannot add or remove elements from the list, or modify the original array without affecting the list.
  • It requires an external dependency on the Guava library. This means that you have to add the library to your project and manage its version and compatibility. This can be an issue if you want to keep your project lightweight or avoid conflicts with other libraries.

4. Comparing with other methods in Java

You can use the Stream API to convert arrays of primitive type int to lists of wrapper type Integer in Java. You can use the Arrays.stream() method to create a stream of int values from the array, and then use the boxed() method to convert them to Integer objects, and then use the collect() method to collect them into a list. For example:

Download Code

 
You can also use a for-each loop to iterate over the elements of the array and add them to a new list. For example:

Download Code

That’s all about Guava Ints.asList() method in Java. For more information about this method, you can check out the Guava official documentation or its GitHub repository.