Usually we nee convert between primitives array to wrapper list, for example: convert int[] to List.
Solution 1: Convert them manully
We can write code to convert them manually like below:
Solution 2: Using Guava primitives
We can use utils in Guava com.google.common.primitives to easily convert between them: the code is cleaner, also its performance is better.
then add all ints to that list, instead, it creates IntArrayAsList which holds a reference to the original int array, its get method creates and returns the wrapper object.
We can't add new element or remove element to the IntArrayAsList.
If your project doesn't use Guava, you may use Apache Commons Lang:
Guava Primitives Explained
We can write code to convert them manually like below:
public void intArrayIntListManully() { int[] aInts = {1, 2, 3}; // int array to ListBut it's kind of annoy to have to write this boilerplate code again and again.List aList = new ArrayList (aInts.length); for (int i : aInts) { aList.add(i); } System.out.println(aList); // this throws UnsupportedOperationException // aList.add(4); // aList.remove(0); // List to int array aInts = new int[aList.size()]; for (int i = 0; i < aList.size(); i++) { aInts[i] = aList.get(i); } System.out.println(aInts.length); }
Solution 2: Using Guava primitives
We can use utils in Guava com.google.common.primitives to easily convert between them: the code is cleaner, also its performance is better.
public void intArrayIntListViaGuava() { int[] aInts = {1, 2, 3}; List<Integer> aList = Ints.asList(aInts); System.out.println(aList); aInts = Ints.toArray(aList); System.out.println(aInts.length); }If we check Guava's implementation, Ints.asList doesn't create one ArrayList
We can't add new element or remove element to the IntArrayAsList.
public static List<Integer> asList(int[] backingArray) { if (backingArray.length == 0) { return Collections.emptyList(); } return new IntArrayAsList(backingArray); } public static int[] toArray(Collection<? extends Number> collection) { if (collection instanceof IntArrayAsList) { return ((IntArrayAsList)collection).toIntArray(); } Object[] boxedArray = collection.toArray(); int len = boxedArray.length; int[] array = new int[len]; for (int i = 0; i < len; ++i) { array[i] = ((Number)Preconditions.checkNotNull(boxedArray[i])).intValue(); } return array; } private static class IntArrayAsList extends AbstractList<Integer> implements RandomAccess, Serializable { final int[] array; final int start; final int end; IntArrayAsList(int[] array) { this(array, 0, array.length); } IntArrayAsList(int[] array, int start, int end) { this.array = array; this.start = start; this.end = end; } public Integer get(int index) { Preconditions.checkElementIndex(index, size()); return Integer.valueOf(this.array[(this.start + index)]); } }Solution 3: Using Apache Commons Lang
If your project doesn't use Guava, you may use Apache Commons Lang:
public void intArrayIntListViaCommonsLang() { int[] aInts = {1, 2, 3}; List<Integer> aList = Arrays.asList(ArrayUtils.toObject(aInts)); System.out.println(aList); aInts = ArrayUtils.toPrimitive(aList.toArray(new Integer[aList.size()])); System.out.println(aInts.length); }Reference
Guava Primitives Explained