Streams
- In Java 8+ you can make a stream of your
intarray. Call eitherArrays.streamorIntStream.of. - Call
IntStream#boxedto use boxing conversion fromintprimitive toIntegerobjects. - Collect into a list using
Stream.collect( Collectors.toList() ). Or more simply in Java 16+, callStream#toList().
Example:
int[] ints = {1,2,3};
List<Integer> list = Arrays.stream(ints).boxed().collect(Collectors.toList());
In Java 16 and later:
List<Integer> list = Arrays.stream(ints).boxed().toList();
Answer from mikeyreilly on Stack OverflowStreams
- In Java 8+ you can make a stream of your
intarray. Call eitherArrays.streamorIntStream.of. - Call
IntStream#boxedto use boxing conversion fromintprimitive toIntegerobjects. - Collect into a list using
Stream.collect( Collectors.toList() ). Or more simply in Java 16+, callStream#toList().
Example:
int[] ints = {1,2,3};
List<Integer> list = Arrays.stream(ints).boxed().collect(Collectors.toList());
In Java 16 and later:
List<Integer> list = Arrays.stream(ints).boxed().toList();
There is no shortcut for converting from int[] to List<Integer> as Arrays.asList does not deal with boxing and will just create a List<int[]> which is not what you want. You have to make a utility method.
int[] ints = {1, 2, 3};
List<Integer> intList = new ArrayList<Integer>(ints.length);
for (int i : ints)
{
intList.add(i);
}
How do I convert Integer[] to int[] and back?
How do I convert an int array to an ArrayList specifically?
Why does Arrays.asList(intArray) return a list with one element?
ArrayList is not the best storage for primitives such as int. ArrayList stores objects not primitives. There are libraries out there for Lists with primitives.
If you really want to store all primitive int as anInteger object you will have to convert them one by one using Integer.valueOf(...) (so you don't create extra Integer instances when not needed).
While you can do this:
List<Integer> list = Arrays.asList(1, 2, 3);
You cannot do this:
int[] i = { 1, 2, 3 };
List<Integer> list = Arrays.asList(i); // this will get you List<int[]>
as an array is actually an object as well an treated as such in var-args when using primitives such as int. Object arrays works though:
List<Object> list = Arrays.asList(new Object[2]);
So something along the lines of:
int[] array = { 1, 2, 3 };
ArrayList<Integer> list = new ArrayList<Integer>(array.length);
for (int i = 0; i < array.length; i++)
list.add(Integer.valueOf(array[i]));
Use Arrays#asList(T... a) to create "a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)"
Integer[] intArray = {1, 2, 3, 42}; // cannot use int[] here
List<Integer> intList = Arrays.asList(intArray);
Alternatively, to decouple the two data structures:
List<Integer> intList = new ArrayList<Integer>(intArray.length);
for (int i=0; i<intArray.length; i++)
{
intList.add(intArray[i]);
}
Or even more tersely:
List<Integer> intList = new ArrayList<Integer>(Arrays.asList(intArray));
In your example, it is because you can't have a List of a primitive type. In other words, List<int> is not possible.
You can, however, have a List<Integer> using the Integer class that wraps the int primitive. Convert your array to a List with the Arrays.asList utility method.
Integer[] numbers = new Integer[] { 1, 2, 3 };
List<Integer> list = Arrays.asList(numbers);
See this code run live at IdeOne.com.
In Java 8, you can use streams:
int[] numbers = new int[] { 1, 2, 3 };
Arrays.stream(numbers)
.boxed()
.collect(Collectors.toList());
Solution for Java 8. A bit longer than the Guava one, but at least you don't have to install a library.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
//...
List<Integer> integers = Arrays.asList(1, 2, 3, 4);
List<String> strings = integers.stream().map(Object::toString)
.collect(Collectors.toList());
For Java 11,
List<String> strings = integers.stream().map(Object::toString)
.collect(Collectors.toUnmodifiableList());
Still no map convenience method, really?
Using Google Collections from Guava-Project, you could use the transform method in the Lists class
import com.google.common.collect.Lists;
import com.google.common.base.Functions
List<Integer> integers = Arrays.asList(1, 2, 3, 4);
List<String> strings = Lists.transform(integers, Functions.toStringFunction());
The List returned by transform is a view on the backing list - the transformation will be applied on each access to the transformed list.
Be aware that Functions.toStringFunction() will throw a NullPointerException when applied to null, so only use it if you are sure your list will not contain null.