With streams added in Java 8 we can write code like:

int[] example1 = list.stream().mapToInt(i->i).toArray();
// OR
int[] example2 = list.stream().mapToInt(Integer::intValue).toArray();

Thought process:

  • The simple Stream#toArray returns an Object[] array, so it is not what we want. Also, Stream#toArray(IntFunction<A[]> generator) which returns A[] doesn't do what we want, because the generic type A can't represent the primitive type int

  • So it would be nice to have some kind of stream which would be designed to handle primitive type int instead of the reference type like Integer, because its toArray method will most likely also return an int[] array (returning something else like Object[] or even boxed Integer[] would be unnatural for int). And fortunately Java 8 has such a stream which is IntStream

  • So now the only thing we need to figure out is how to convert our Stream<Integer> (which will be returned from list.stream()) to that shiny IntStream.

    Quick searching in documentation of Stream while looking for methods which return IntStream points us to our solution which is mapToInt(ToIntFunction<? super T> mapper) method. All we need to do is provide a mapping from Integer to int.

    Since ToIntFunction is functional interface we can also provide its instance via lambda or method reference.

    Anyway to convert Integer to int we can use Integer#intValue so inside mapToInt we can write:

    mapToInt( (Integer i) -> i.intValue() )
    

    (or some may prefer: mapToInt(Integer::intValue).)

    But similar code can be generated using unboxing, since the compiler knows that the result of this lambda must be of type int (the lambda used in mapToInt is an implementation of the ToIntFunction interface which expects as body a method of type: int applyAsInt(T value) which is expected to return an int).

    So we can simply write:

    mapToInt((Integer i)->i)
    

    Also, since the Integer type in (Integer i) can be inferred by the compiler because List<Integer>#stream() returns a Stream<Integer>, we can also skip it which leaves us with

    mapToInt(i -> i)
    
Answer from Pshemo on Stack Overflow
Top answer
1 of 16
1073

With streams added in Java 8 we can write code like:

int[] example1 = list.stream().mapToInt(i->i).toArray();
// OR
int[] example2 = list.stream().mapToInt(Integer::intValue).toArray();

Thought process:

  • The simple Stream#toArray returns an Object[] array, so it is not what we want. Also, Stream#toArray(IntFunction<A[]> generator) which returns A[] doesn't do what we want, because the generic type A can't represent the primitive type int

  • So it would be nice to have some kind of stream which would be designed to handle primitive type int instead of the reference type like Integer, because its toArray method will most likely also return an int[] array (returning something else like Object[] or even boxed Integer[] would be unnatural for int). And fortunately Java 8 has such a stream which is IntStream

  • So now the only thing we need to figure out is how to convert our Stream<Integer> (which will be returned from list.stream()) to that shiny IntStream.

    Quick searching in documentation of Stream while looking for methods which return IntStream points us to our solution which is mapToInt(ToIntFunction<? super T> mapper) method. All we need to do is provide a mapping from Integer to int.

    Since ToIntFunction is functional interface we can also provide its instance via lambda or method reference.

    Anyway to convert Integer to int we can use Integer#intValue so inside mapToInt we can write:

    mapToInt( (Integer i) -> i.intValue() )
    

    (or some may prefer: mapToInt(Integer::intValue).)

    But similar code can be generated using unboxing, since the compiler knows that the result of this lambda must be of type int (the lambda used in mapToInt is an implementation of the ToIntFunction interface which expects as body a method of type: int applyAsInt(T value) which is expected to return an int).

    So we can simply write:

    mapToInt((Integer i)->i)
    

    Also, since the Integer type in (Integer i) can be inferred by the compiler because List<Integer>#stream() returns a Stream<Integer>, we can also skip it which leaves us with

    mapToInt(i -> i)
    
2 of 16
265

Unfortunately, I don't believe there really is a better way of doing this due to the nature of Java's handling of primitive types, boxing, arrays and generics. In particular:

  • List<T>.toArray won't work because there's no conversion from Integer to int
  • You can't use int as a type argument for generics, so it would have to be an int-specific method (or one which used reflection to do nasty trickery).

I believe there are libraries which have autogenerated versions of this kind of method for all the primitive types (i.e. there's a template which is copied for each type). It's ugly, but that's the way it is I'm afraid :(

Even though the Arrays class came out before generics arrived in Java, it would still have to include all the horrible overloads if it were introduced today (assuming you want to use primitive arrays).

🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-integer-list-to-integer-array
Java Program to Convert Integer List to Integer Array - GeeksforGeeks
July 23, 2025 - Apache Commons Lang’s ArrayUtils class provides toPrimitive() method that can convert an Integer array to primitive ints. We need to convert a list of integers to an Integer array first. We can use List.toArray() for easy conversion. Procedure: Use toPrimtive() method of Apache Common Lang's · Use List.toArray() method · Example: Java ·
🌐
Mkyong
mkyong.com › home › java › java – convert int[] to integer[] example
Java - Convert int[] to Integer[] example - Mkyong.com
February 22, 2014 - package com.mkyong.test; import org.apache.commons.lang3.ArrayUtils; public class ArrayConvertExample { public static void main(String[] args) { int[] obj = new int[] { 1, 2, 3 }; Integer[] newObj = ArrayUtils.toObject(obj); System.out.println("Test toObject() - int -> Integer"); for (Integer temp : newObj) { System.out.println(temp); } Integer[] obj2 = new Integer[] { 4, 5, 6 }; int[] newObj2 = ArrayUtils.toPrimitive(obj2); System.out.println("Test toPrimitive() - Integer -> int"); for (int temp : newObj2) { System.out.println(temp); } } Test toObject() - int -> Integer 1 2 3 Test toPrimitive() - Integer -> int 4 5 6 ... Founder of Mkyong.com, passionate Java and open-source technologies.
🌐
Jackrutorial
jackrutorial.com › 2019 › 03 › how-to-convert-int-array-to-integer-in-java.html
How to convert an int array to an integer in Java - Java Programming Examples - Learning to Write code for Beginners with Tutorials
package com.jackrutorial; public class ArrayToIntegers1 { public static void main(String[] args) { int intArrs[] = { 2, 0, 1, 9 }; System.out.print("int arrays: ["); for (int i=0; i< intArrs.length; i++) { System.out.print(intArrs[i]); if(i < intArrs.length-1) { System.out.print(","); } } System.out.print("]"); StringBuilder builder = new StringBuilder(); for (int num : intArrs) { builder.append(num); } int number = Integer.parseInt(builder.toString()); System.out.print(" -> converted: "); System.out.println(number); } }
🌐
BeginnersBook -
beginnersbook.com › home › java › convert integer list to int array in java
Convert Integer List to int Array in Java
September 23, 2022 - Here, we are using toArray() method of ArrayList class to convert the given Integer ArrayList to int array. import java.util.*; public class JavaExample { public static void main(String[] args) { //A List of integers List<Integer> list= new ArrayList<>(); list.add(2); list.add(4); list.add(6); ...
🌐
Processing Forum
forum.processing.org › topic › how-do-i-convert-an-int-array-to-an-integer
How do i convert an int array to an integer? - Processing Forum
For example I have an array of integers: int[] array =new int[7]; I then put integers in few slots of the array and now I want all those slots to be concatenated into one integer so for example if my array was: array[0] = 1 array[1] = 3 array[2] = 5 array[3] = 6 i want a new integer lets say ...
Find elsewhere
🌐
Coderanch
coderanch.com › t › 404571 › java › Integer-array-int-array
Integer array to int array (Beginning Java forum at Coderanch)
I daresay that is no possible. If what you actually mean is that you do not want to write the algoritm then use Jakarta Commons ArrayUtils class, it has a method int[] toPrimitive(Integer[]) And this is what the algorithm does: I hope that helps!
🌐
Quora
quora.com › How-do-I-convert-int-to-int-array-of-digts
How to convert int to int [] (array of digts) - Quora
Answer (1 of 12): Well, one way to do that is to turn the int into a string first, then loop through each character, converting them back to integers and sticking them in an array—like splitting the number into its digits step by step.
🌐
Coderanch
coderanch.com › t › 416619 › java › Convert-Integer-List-int-array
Convert Integer List to int array (Beginning Java forum at Coderanch)
Looping through the list and copying the elements into an array is the only way to do this. ... if you want to change a List<Integer> into an Integer[] you can try the Collection.toArray(T[]) method. This uses generics so is java1.5 and later only. Have a look at the javadoc for this to understand ...
🌐
Coderanch
coderanch.com › t › 484374 › java › Double-array-int-array
Double array to int array? [Solved] (Beginning Java forum at Coderanch)
Unfortunately System.arraycopy() won't help you here and neither will any of the utility methods provided by the java.util.Arrays class, so you'll have to do code the loop-convert-store routine yourself. Edit: Sloooooooow. Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life. ... Otherwise, the loop will start with element 1 of the array rather than element 0.
🌐
Reddit
reddit.com › r/learnjava › convert list into an array in java
r/learnjava on Reddit: Convert List into an array in Java
July 17, 2020 -

Suppose we have a List<Employee> object and we want to convert it into Employee[ ].

List<Employee> list=new ArrayList<>();

We can convert list to object type array in following ways:

Employee[] empArray = list.toArray(new Employee[0]);

or

Employee[] empArray = new Employee[list.size()];

list.toArray(empArray);

But to convert arrays to primitive types. you have to convert it into following way:-

List<Integer> list = ...;

int[] array = new int[list.size()];

for(int i = 0; i < list.size(); i++) array[i] = list.get(i);

Is there any other way to convert List to its specific type of array?

🌐
Blogger
javarevisited.blogspot.com › 2013 › 05 › how-to-convert-list-of-integers-to-int-array-java-example-tips.html
How to Convert List of Integers to Int Array in Java - Example
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.
🌐
sebhastian
sebhastian.com › int-to-int-array-java
How to convert Java Integer to Int Array instance | sebhastian
July 8, 2021 - for (int i = 0; i < temp.length(); ... content: [49, 50, 51, 52, 53] That’s why you need to wrap the value returned by charAt() method with getNumericalValue() method....
🌐
W3Docs
w3docs.com › java
How to convert int[] into List<Integer> in Java?
To convert an int[] array into a List<Integer> in Java, you can use the Arrays.stream() method to create a stream of the array, and then use the mapToObj() method to map each element of the stream to an Integer object.
🌐
Baeldung
baeldung.com › home › java › java array › converting a string array into an int array in java
Converting a String Array Into an int Array in Java | Baeldung
January 8, 2024 - In this article, we’ve learned two ways to convert a string array to an integer array through examples. Moreover, we’ve discussed handling the conversion when the string array contains invalid number formats. If our Java version is 8 or later, the Stream API would be the most straightforward solution to the problem.
🌐
Studytonight
studytonight.com › java-examples › convert-integer-list-to-int-array-in-java
Convert Integer List to Int Array in Java - Studytonight
To change this ArrayList to the Array we called stream().mapToInt().toAttay() method on the list and the result of this will be assigned to array arr. import java.util.*; public class StudyTonight { public static void main(String[] args) { ...