I\'m trying to convert an ArrayList containing Integer objects to primitive int[] with the following piece of code, but it is throwing compile time error. Is it possible to
If you're using Eclipse Collections, you can use the collectInt()
method to switch from an object container to a primitive int container.
List<Integer> integers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
MutableIntList intList =
ListAdapter.adapt(integers).collectInt(i -> i);
Assert.assertArrayEquals(new int[]{1, 2, 3, 4, 5}, intList.toArray());
If you can convert your ArrayList
to a FastList
, you can get rid of the adapter.
Assert.assertArrayEquals(
new int[]{1, 2, 3, 4, 5},
Lists.mutable.with(1, 2, 3, 4, 5)
.collectInt(i -> i).toArray());
Note: I am a committer for Eclipse collections.
If you are using java-8 there's also another way to do this.
int[] arr = list.stream().mapToInt(i -> i).toArray();
What it does is:
Stream<Integer>
from the listIntStream
by mapping each element to itself (identity function), unboxing the int
value hold by each Integer
object (done automatically since Java 5)int
by calling toArray
You could also explicitly call intValue
via a method reference, i.e:
int[] arr = list.stream().mapToInt(Integer::intValue).toArray();
It's also worth mentioning that you could get a NullPointerException
if you have any null
reference in the list. This could be easily avoided by adding a filtering condition to the stream pipeline like this:
//.filter(Objects::nonNull) also works
int[] arr = list.stream().filter(i -> i != null).mapToInt(i -> i).toArray();
Example:
List<Integer> list = Arrays.asList(1, 2, 3, 4);
int[] arr = list.stream().mapToInt(i -> i).toArray(); //[1, 2, 3, 4]
list.set(1, null); //[1, null, 3, 4]
arr = list.stream().filter(i -> i != null).mapToInt(i -> i).toArray(); //[1, 3, 4]
This code segment is working for me, try this:
Integer[] arr = x.toArray(new Integer[x.size()]);
Worth to mention ArrayList should be declared like this:
ArrayList<Integer> list = new ArrayList<>();
It bewilders me that we encourage one-off custom methods whenever a perfectly good, well used library like Apache Commons has solved the problem already. Though the solution is trivial if not absurd, it is irresponsible to encourage such a behavior due to long term maintenance and accessibility.
Just go with Apache Commons
Apache Commons has a ArrayUtils class, which has a method toPrimitive() that does exactly this.
import org.apache.commons.lang.ArrayUtils;
...
List<Integer> list = new ArrayList<Integer>();
list.add(new Integer(1));
list.add(new Integer(2));
int[] intArray = ArrayUtils.toPrimitive(list.toArray(new Integer[0]));
However, as Jon showed, it is pretty easy to do this by yourself instead of using external libraries.
Next lines you can find convertion from int[] -> List -> int[]
private static int[] convert(int[] arr) {
List<Integer> myList=new ArrayList<Integer>();
for(int number:arr){
myList.add(number);
}
}
int[] myArray=new int[myList.size()];
for(int i=0;i<myList.size();i++){
myArray[i]=myList.get(i);
}
return myArray;
}