Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an ArrayList containing Integers to primitive int array?

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 convert in Java?

List<Integer> x =  new ArrayList<Integer>(); int[] n = (int[])x.toArray(int[x.size()]); 
like image 819
Snehal Avatar asked Apr 05 '09 07:04

Snehal


People also ask

How do I return an ArrayList to an int array?

To convert ArrayList to array in Java, we can use the toArray(T[] a) method of the ArrayList class. It will return an array containing all of the elements in this list in the proper order (from first to last element.) Here's a short example to convert an ArrayList of integers, numbersList , to int array.

Can an ArrayList contain elements with primitive data types?

Primitive data types cannot be stored in ArrayList but can be in Array.

How will you convert a list into an array of integers like int []?

We can use the Stream provided by Java 8 to convert a list of Integer to a primitive integer array in Java. We start by converting given List<Integer> to Stream<Integer> using List. stream() method. Now all we need to do is convert Stream<Integer> to int[] .


2 Answers

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:

  • getting a Stream<Integer> from the list
  • obtaining an IntStream by mapping each element to itself (identity function), unboxing the int value hold by each Integer object (done automatically since Java 5)
  • getting the array of 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] 
like image 121
Alexis C. Avatar answered Oct 13 '22 23:10

Alexis C.


You can convert, but I don't think there's anything built in to do it automatically:

public static int[] convertIntegers(List<Integer> integers) {     int[] ret = new int[integers.size()];     for (int i=0; i < ret.length; i++)     {         ret[i] = integers.get(i).intValue();     }     return ret; } 

(Note that this will throw a NullPointerException if either integers or any element within it is null.)

EDIT: As per comments, you may want to use the list iterator to avoid nasty costs with lists such as LinkedList:

public static int[] convertIntegers(List<Integer> integers) {     int[] ret = new int[integers.size()];     Iterator<Integer> iterator = integers.iterator();     for (int i = 0; i < ret.length; i++)     {         ret[i] = iterator.next().intValue();     }     return ret; } 
like image 24
Jon Skeet Avatar answered Oct 14 '22 00:10

Jon Skeet