Convert a collection of numbers to an int array in Java
This post will discuss how to convert a collection of numbers to an int array in Java.
1. Using a Loop
One possible solution is to use a loop and manually copy each element from the collection to the array. This is the most basic and straightforward way, but it requires writing more code and handling possible null values or exceptions manually. For example, if you have a Collection<? extends Integer>, you can do something like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.Collection; class Main { public static void main(String[] args) { Collection<Integer> collection = Arrays.asList(1, 2, 3, 4, 5); int[] arr = new int[collection.size()]; int i = 0; for (Number n : collection) { if (n == null) { throw new NullPointerException(); } arr[i++] = n.intValue(); } System.out.println(Arrays.toString(arr)); // [1, 2, 3, 4, 5] } } |
This solution works, but it is verbose and error-prone. You have to create the array with the correct size, iterate over the collection using an index variable, and handle possible null values or exceptions.
2. Using a Stream
This is a more functional and expressive way, but it requires using the Java 8 Stream API, which may not be available or familiar to some developers. It also creates a third array internally using the toArray() method. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.Collection; class Main { public static void main(String[] args) { Collection<Integer> collection = Arrays.asList(1, 2, 3, 4, 5); int[] arr = collection.stream() .mapToInt(Number::intValue) .toArray(); System.out.println(Arrays.toString(arr)); // [1, 2, 3, 4, 5] } } |
3. Using Apache Commons ArrayUtils
This is a third-party library that provides various methods for working with arrays, including converting wrapper arrays to primitive arrays. It can be used in combination with the Collection.toArray() method, but it also requires adding another dependency to the project. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; import java.util.Collection; class Main { public static void main(String[] args) { Collection<Integer> collection = Arrays.asList(1, 2, 3, 4, 5); Integer[] wrapperArr = collection.toArray(new Integer[0]); int[] arr = ArrayUtils.toPrimitive(wrapperArr); System.out.println(Arrays.toString(arr)); // [1, 2, 3, 4, 5] } } |
4. Using Ints
The Guava Ints.toArray() method is a utility method that converts a collection of Integer objects to a primitive int array. It takes a collection of Number instances as a parameter and returns an array containing the same values as the collection, in the same order, converted to primitives. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import com.google.common.primitives.Ints; import java.util.Arrays; import java.util.Collection; class Main { public static void main(String[] args) { Collection<Integer> collection = Arrays.asList(1, 2, 3, 4, 5); int[] arr = Ints.toArray(collection); System.out.println(Arrays.toString(arr)); // [1, 2, 3, 4, 5] } } |
That’s all about converting a collection of numbers to an int array 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 :)