Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Integer[] to int[] array in Java?

Is there a fancy way to cast an Integer array to an int array? (I don't want to iterate over each element; I'm looking for an elegant and quick way to write it)

The other way around I'm using

scaleTests.add(Arrays.stream(data).boxed().toArray(Double[]::new));

I'm looking for an one-liner but wasn't able to find something.

The goal is to:

int[] valuesPrimitives = <somehow cast> Integer[] valuesWrapper 
like image 338
Michael Brenndoerfer Avatar asked Jul 13 '15 22:07

Michael Brenndoerfer


People also ask

What is the difference between int array [] and int [] array?

What is the difference between int[] a and int a[] in Java? There is no difference in these two types of array declaration. There is no such difference in between these two types of array declaration. It's just what you prefer to use, both are integer type arrays.

Can we convert int to array in Java?

The code line Array. from(String(numToSeparate), Number); will convert the number into a string, take each character of that string, convert it into a number and put in a new array. Finally, this new array of numbers will be returned.


1 Answers

You can use Stream APIs of Java 8

int[] intArray = Arrays.stream(array).mapToInt(Integer::intValue).toArray(); 
like image 197
Vaibhav Avatar answered Sep 17 '22 13:09

Vaibhav