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
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ java โ€บ convert int array to integer array in java
Convert int array to Integer array in Java | Techie Delight
July 7, 2026 - We can also use IntStream.of() to get IntStream from integer array. We can also use Guava API to convert a primitive integer array to the Integer array.
๐ŸŒ
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); } }
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ java array โ€บ convert int[] to integer[] in java and back to int[]
Convert int[] to Integer[] in Java and Back to int[]
February 10, 2022 - Java has no cast between int[] and Integer[], so to convert int[] to Integer[] we box every element into a new array, most often with Arrays.stream(ints).boxed().toArray(Integer[]::new), and to go back we unbox every element with mapToInt(I...
๐ŸŒ
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
March 22, 2019 - 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); } }
๐ŸŒ
sebhastian
sebhastian.com โ€บ int-to-int-array-java
How to convert Java Integer to Int Array instance | sebhastian
July 8, 2021 - To convert an Integer or int type data into an int[] array type, you need to do the following steps: ... Then create a new int[] instance and use the value of String.length() method as the size of the array
Find elsewhere
Top answer
1 of 16
1076

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).

๐ŸŒ
YouTube
youtube.com โ€บ jack rutorial
Convert an int array to an integer in Java Example - YouTube
In this example, we show you how to convert an int array to an integer in JavaSource code: http://bit.ly/2JDgKNKPHP Code: https://soltuts.com/tag/php/
Published: March 25, 2019
Views: 4K
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 404571 โ€บ java โ€บ Integer-array-int-array
Integer array to int array (Beginning Java forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums ยท this forum made possible by our volunteer staff, including ... ... Hi all, Is there a way to transform an Integer[] to an int[] and vice-versa without looping through the array ?
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 378445 โ€บ java โ€บ Convert-Integer-int
Convert Integer[] to int[] (Java in General forum at Coderanch)
Originally posted by Hans Dampf: Hello, does anybody know how I can convert an array of Integers into an array of ints without looping over every array element? Thank You very much. Regards, Hans No, nobody does.
๐ŸŒ
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 : ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 416619 โ€บ java โ€บ Convert-Integer-List-int-array
Convert Integer List to int array (Beginning Java forum at Coderanch)
September 23, 2008 - 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. Tom Silverman: SCJP5, SCJD6, SCWCD5, SCBCD5, IBM-142, ScrumMaster ... 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.
๐ŸŒ
ExamClouds
examclouds.com โ€บ home โ€บ java core โ€บ convert int array to list and back in java
Convert int Array to List<Integer> and ArrayList in Java
September 16, 2026 - Swap the collector and you get a Set, a LinkedList or any other Collection โ€” and stream().mapToInt().toArray() converts every one of them back to int[]. import java.util.LinkedList; import java.util.Set; int[] numbers = {3, 1, 2, 3}; // int[] ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ 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 { public static void main(String[] args) { ArrayList<Integer>arrList = new ArrayList<Integer>(); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500); arrList.add(600); arrList.add(700); arrList.add(800); arrList.add(900); arrList.add(1000); final int[] arr = new int[arrList.size()]; int index = 0; for (final Integer value : arrList) { arr[index++] = value; } System.out.println("Elements of int array..."); for (Integer i : arr) { System.out.println(i); } } }
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-convert-integer-list-to-int-array-117397
Java Integer List to Int Array Conversion | Programming Tutorials | LabEx
We often collect numbers in a List (which requires Integer), but some methods need a primitive int array for better performance. This is why we need to convert between them. Let's modify our IntegerListToIntArray.java file to create and display a list of numbers: