Home > DeveloperSection > Forums > Converting from <Datatype1>[] to <Datatype2>[]
Jayne Spooner
Jayne Spooner

Total Post:21

Posted on    July-09-2015 7:03 AM

 Java Java  Collection  Array 

 1 Reply(s)
 779  View(s)
Rate this:
Is it possible to convert from a list of datatypes without going through the trouble of looping through the entire list.

I.e:b = {"1", "2"}; 
Integer[] a = (Integer[]) b; //not actually runnable

Not really necessary, but it would be nice.

Mayank Tripathi
Mayank Tripathi

Total Post:397

Posted on    July-09-2015 8:17 AM

In Java 8, you can do
Integer[] integerArray = 
If you want an int-Array, you can do:
int[] intArray = 
Stream.of() creates a stream from the array. map maps every element of the stream to an Integer using Integer::parseInt. Using mapToInt, you can create an Intstream, which is a specialization of streams for integer primitives. Integer[]::new is a method reference to an array constructor.

This assumes all elements of the array are parseable, if not it will throw an exception.

Don't want to miss updates? Please click the below button!

Follow MindStick