Guava Ints.asList() method in Java
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:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { Integer[] intArray = {1, 2, 3, 4, 5}; List<Integer> list = Arrays.asList(intArray); System.out.println(list); // [1, 2, 3, 4, 5] } } |
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:
|
1 2 3 4 5 6 7 8 9 10 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { int[] intArray = {1, 2, 3, 4, 5}; List<int[]> list = Arrays.asList(intArray); // not what you want System.out.println(list); } } |
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:
|
1 |
public static List<Integer> asList(int[] backingArray) |
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:
|
1 2 3 4 5 6 7 8 9 10 |
import com.google.common.primitives.Ints; import java.util.List; class Main { public static void main(String[] args) { int[] intArray = {1, 2, 3, 4, 5}; List<Integer> list = Ints.asList(intArray); System.out.println(list); // [1, 2, 3, 4, 5] } } |
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 typeInteger. 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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { int[] intArray = {1, 2, 3, 4, 5}; List<Integer> list = Arrays.stream(intArray) .boxed() .collect(Collectors.toList()); System.out.println(list); // [1, 2, 3, 4, 5] } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.ArrayList; import java.util.List; class Main { public static void main(String[] args) { int[] intArray = {1, 2, 3, 4, 5}; List<Integer> list = new ArrayList<>(); for (int i : intArray) { list.add(i); } System.out.println(list); // [1, 2, 3, 4, 5] } } |
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.
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 :)