Efficient way to convert List <Integer> to int [] (array) without iteration
public static int[] convertListToArray(List<Integer> listResult) { int[] result = new int[listResult.size()]; int i= 0; for (int num : listResult) { result[i++] = num; } return result; } Is there an efficient way to convert a list to an array without iterating the list explicitly? Perhaps this is possible using methods such as:
Arrays.copyOf(int [] origin , int newLength ); System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length); I know the solution is described here. However, I'm particularly interested in the efficient way to convert List<Integer> to int[]
Given the need to convert from Integer to int , I donβt think that you will find something more efficient than what you have if I assume that you are talking about execution efficiency.
You could find the conversion in Integer[] , and then the loop may be more efficient (below), but you may not . You will have to test it in your specific scenario and see.
Here is an example:
int size = listResult.size(); int[] result = new int[size]; Integer[] temp = listResult.toArray(new Integer[size]); for (int n = 0; n < size; ++n) { result[n] = temp[n]; } If efficiency is your primary concern, I think you can use your solution and make it more efficient using the indexed for loop in listResult if it is RandomAccess . However, this makes the code much less readable, and you will need to compare it for your use cases to make sure it is more efficient.
public static int[] convertListToArray(List<Integer> listResult) { int size = listResult.size(); int[] result = new int[size]; if (listResult instanceof RandomAccess) { for (int i = 0; i < size; i++) { result[i] = listResult.get(i); } } else { int i = 0; for (int num : listResult) { result[i++] = num; } } return result; } If you use Java 8 and want to write less code, you can use the Streams library.
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int[] array = list.stream().mapToInt(i -> i).toArray(); If you are open to using a third-party library, you can Eclipse Collections as follows.
MutableList<Integer> list = Lists.mutable.with(1, 2, 3, 4, 5); int[] array = list.collectInt(i -> i).toArray(); Below is a bit more code, but this is the most efficient solution I could use with Eclipse Collections.
MutableList<Integer> list = Lists.mutable.with(1, 2, 3, 4, 5); int[] array = new int[list.size()]; list.forEachWithIndex((each, index) -> array[index] = each); If you need to use the java.util.List interface, you can use ListIterate from Eclipse Collections.
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int[] array = new int[list.size()]; ListIterate.forEachWithIndex(list, (each, index) -> array[index] = each); The ListIterate utility will use a different iteration code for RandomAccess and not RandomAccess .
The most efficient task would be to change the List<Integer> to MutableIntList in Eclipse collections or in another library supporting for primitive collections.
Note. I am a committer for Eclipse collections.
In Java 8:
int[] anArray = list.stream() .filter(Objects::nonNull) .mapToInt(Integer::intValue) .toArray();