Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Tuesday, July 27, 2021

How to Convert List of Integers to Int Array in Java - Example

So, you have a List of Integers and you want to convert them into int array? Yes, you read it write, not on Integer array but int array. Though in most practical purpose, Integer array can be used in place of int[] because of autoboxing in Java, you still need an int[] if your method accepts it. In Java, you can not typecast an Integer array into an int array. Many Java programmer think about toArray() method from java.util.List to convert a List into Array, but unfortunately, toArray() is useless most of the time. It doesn't allow you to convert List into primitive arrays.

Though you can convert List of Integers to an array of Integers, not array of primitive int. This is true for List of all wrapper class e.g. List of Float, Double, Long, toArray() method can return an array of wrapper class but not primitives.

After looking into Java Collection API, It seems that only traditional for loop or foreach can help, which involves iterating over Integer array and storing them into int[], but fortunately, I came across Apache Commons ArrayUtils class. ArrayUtils can do this work for us, It has several overloaded methods to convert Object arrays into primitive arrays. 

For example, you can convert an array of Double i.e. Double[] to primitive double array e.g. double[]. This example once again reinforced my thinking to include Apache Commons lang and Google's Guava by default in any Java project, they are rich and effectively complement standard JDK library.

List of Integer to int array in Java

Java Tip to convert List of Integers to int array in Java exampleHere is a complete Java program to convert Integer List into int array. We have first used the toArray() method to convert List into Array of the same type, and then we use ArrayUtils to convert wrapper class array into a primitive array. By the way, if you don't like to add dependencies on your project, you can also use simple for loop to iterate over the Integer array and storing them back into an int[]. 

here is the code.
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;

 * Java program to convert List of Integers into int array using 
 * Apache commons ArrayUtils class.
 * @author Javin Paul
public class IntegerListToIntArray {
    public static void main(String args[]) {
       List<Integer> numbers = Arrays.asList(1,2,3,4,5,6);
       System.out.println("List of Integers : " + numbers);
       // toArray() can return Integer array but not int array
       Integer[] integers = numbers.toArray(new Integer[numbers.size()]);
       // ArrayUtils of Apache Commons can change an Object array to primitive array
       int[] primitives = ArrayUtils.toPrimitive(integers);
       // Let's see what does this int array contains
       System.out.println("Array with primitive int : " + Arrays.toString(primitives));

List of Integers : [1, 2, 3, 4, 5, 6]
Array with primitive int : [1, 2, 3, 4, 5, 6]

As you can see, It's pretty straightforward to convert a List of Integers to an int array, though it requires more than one method call to do that. It would have been much better if toArray() can return primitive arrays as well. 

On a similar note, conversion of Arrays are a bit tricky in Java as int[] != Integer[] , so keep that in mind while writing Java API. Prefer List over Array for public methods, if you had to accept arrays, try to minimize the scope of those methods by making them private or package-private.

Other Java Collection tutorials you may like
  • Difference between ArrayList and HashSet in Java? (answer)
  • The difference between TreeMap and TreeSet in Java? (answer)
  • The difference between HashMap and ConcurrentHashMap in Java? (answer)
  • The difference between HashMap and LinkedHashMap in Java? (answer)
  • The difference between Hashtable and HashMap in Java? (answer)
  • The difference between HashSet and TreeSet in Java? (answer)
  • The difference between ArrayList and LinkedList in Java? (answer)
  • The difference between Vector and ArrayList in Java? (answer)
  • Difference between EnumMap and HashMap in Java?

Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a comment.


Unknown said...

Thanks for posting yet another article. I usually read most of the new ones. I have one comment regarding the format of code you write. It is not readable with black background. Is it in your control to change it?

Anonymous said...

This is what I don't like, there is no direct generic way to convert Collections to array, since array doesn't support Generics, you can not create T[], which means you have to rely on overloaded method to convert list of integers to int array, list of Float to float array, List of Double to double array and so on....

heySourabh said...

It is now possible to use Streams and do this easily: -> i).toArray();

Post a Comment