Native Java 8 (one line)

With Java 8, int[] can be converted to Integer[] easily:

int[] data = {1,2,3,4,5,6,7,8,9,10};

// To boxed array
Integer[] what = Arrays.stream( data ).boxed().toArray( Integer[]::new );
Integer[] ever = IntStream.of( data ).boxed().toArray( Integer[]::new );

// To boxed list
List<Integer> you  = Arrays.stream( data ).boxed().collect( Collectors.toList() );
List<Integer> like = IntStream.of( data ).boxed().collect( Collectors.toList() );

As others stated, Integer[] is usually not a good map key. But as far as conversion goes, we now have a relatively clean and native code.

Answer from Sheepy on Stack Overflow
🌐
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; public class ArrayConvertExample { public static void main(String[] args) { int[] obj = new int[] { 1, 2, 3 }; Integer[] newObj = 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 = toPrimitive(obj2); System.out.println("Test toPrimitive() - Integer -> int"); for (int temp : newObj2) { System.out.println(temp); } } // Convert int[] to Integer[] public static Integer[] toObject(int[] intArray) { Integer[] result = new Integer[intArray.
🌐
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); } }
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-an-object-array-to-an-integer-array-in-java
How to convert an object array to an integer array in Java?
You can convert an object array to an integer array in one of the following ways − · By copying each element from integer array to object array − · import java.util.Arrays; public class ObjectArrayToStringArray { public static void main(String args[]){ Object[] objArray = {21, 58, 69, ...
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).

🌐
DEV Community
dev.to › pfilaretov42 › tiny-how-the-conversion-of-int-to-list-can-be-buggy-21oa
[Tiny] How the conversion of int[] to List<Integer> can be buggy?
June 1, 2023 - The reason is that Arrays.asList(array) does not box int to Integer and creates a List of <int[]>, not a List of Integer as we wanted it to. ... Integer[] array = new Integer[]{42, 5, 1, 3, 4}; List<Integer> list = new ArrayList<>(Arrays.asList(array)); System.out.println(list.get(0)); array can be converted to List<Integer> using IntStream with boxing:
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-integer-array-list-to-integer-array-in-java
How to convert Integer array list to integer array in Java?
We used size() to get the size of the integer array list and placed the same size to the newly created integer array − · final int[] arr = new int[arrList.size()]; int index = 0; for (final Integer value: arrList) { arr[index++] = value; } ... import java.util.ArrayList; public class Demo ...
🌐
Ducmanhphan
ducmanhphan.github.io › 2022-04-17-how-to-convert-int-to-list-integer
How to convert int[] to List of Integer
Because the addAll() method needs the second arguments as the array of Integer. So we will convert the int[] to Integer[], then pass it as the argument. int[] rawData = {1, 3, 5}; List<Integer> data = new ArrayList<Integer>(); for (int num : ...
🌐
Reddit
reddit.com › r/javahelp › how do i convert one element from an array into an int?
r/javahelp on Reddit: How do I convert one element from an array into an int?
April 2, 2021 -

I am making an if statement in a while loop.

I know I need

if (Integer.parseInt(array[i]) == //what do i put here

??

Thank you

Top answer
1 of 3
2
Kinda depends on what you're doing. To just get an integer from an array you would just declare a new variable and say "int x = (int)array[i];" The (int) part casts whatever array[i] is to an int. It'll give you an error if array[i] can't be converted to an int.
2 of 3
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Coderanch
coderanch.com › t › 416619 › java › Convert-Integer-List-int-array
Convert Integer List to int array (Beginning Java forum at Coderanch)
Java treats an array of primitives or objects as an Array so autoboxing will not help here. I think there is just no avoiding doing it manually. ... If you are simply trying to reduce the verbosity of your code then Integer[] can be coded the same way as int [] for accessing elements. This is Autoboxing and Autounboxing working for individual elements of the array.
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-convert-int-array-to-intstream
Java program to convert int array to IntStream
This involves creating an IntStream from the array, limiting the stream to a specific number of elements, and then calculating the sum of those elements. import java.util.Arrays; import java.util.stream.IntStream; public class Demo { public static void main(String[] args) { int[] arr = {10, ...
🌐
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( int temp=0 · ; temp < theDigits · .length; temp++){ result*=10; result+=theDigits[temp]; } tfguy44 · 1 year ago · This assumes, of course, that the numbers you have in the array are digits! Leave a comment on tfguy44's reply · PhiLho · 1 year ago · There can be several strategies for converting an array of integers to a single integer: just pick one, sum them up as tfguy44 shown, but you can also compute the average, the median value, and so on...
🌐
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); ...
🌐
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.
🌐
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.
🌐
Studytonight
studytonight.com › java-examples › convert-integer-list-to-int-array-in-java
Convert Integer List to Int Array in Java - Studytonight
import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.ArrayUtils; public class StudyTonight { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); int[] arr = ArrayUtils.toPrimitive(list.toArray(new Integer[list.size()])); for (int val : arr) { System.out.println("int primitive: "+val); } } } ... We learned how to convert integer list to int array in java.