Create a List of Primitive Int

Create a List of primitive int?

In Java the type of any variable is either a primitive type or a reference type. Generic type arguments must be reference types. Since primitives do not extend Object they cannot be used as generic type arguments for a parametrized type.

Instead use the Integer class which is a wrapper for int:

List<Integer> list = new ArrayList<Integer>();

If your using Java 7 you can simplify this declaration using the diamond operator:

List<Integer> list = new ArrayList<>();

With autoboxing in Java the primitive type int will become an Integer when necessary.

Autoboxing is the automatic conversion that the Java compiler makes
between the primitive types and their corresponding object wrapper
classes.

So the following is valid:

int myInt = 1;
List<Integer> list = new ArrayList<Integer>();
list.add(myInt);

System.out.println(list.get(0)); //prints 1

How to best store a list of primitive values?

Well, Double, Integer, Long all belong to the Number-class. So a

List<Number>

would probably fit. It is exactly what it is - a List of numbers of unspecified subtype. Because of autoboxing you should be able to just add the primitives, but the better practice would be to use the Wrapper-classes.

The Number-class offers methods to get the different representations of the Number, for example doubleValue(). So you could convert all the values in the List<Number> to (as an example) Doubles by using this method. For more reference see the Oracle documentation for Number.

List with primitive array versus List with Object array

Here is what is happening. Your current code is actually creating a List<Object>, which happens to contain just a single int[], not a list of actual integers:

int[] ar = new int[]{1,2};
List<Object> list = Arrays.asList(ar);

The reason for the ArrayIndexOutOfBounds exception is that the list you pass in to the swap method has only one entry, at index zero.

In any case, it is not possible to use Arrays.asList to directly convert an array of primitives to a list of some boxed type. The first version of your code is correct, and is what you should be using:

Integer[] ar = new Integer[] {1, 2};
swap(Arrays.asList(ar), 1, 0);

Java: making List from primitive array using Stream API

Yes this is because Arrays.stream returns an IntStream. You can call boxed() to get a Stream<Integer> and then perform the collect operation.

List<Integer> greaterThan4 = Arrays.stream(values)
.filter(value -> value > 4)
.boxed()
.collect(Collectors.toList());

How to convert int[] into ListInteger in Java?

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);
}

Converting Primitive int array to list

The reason it is not working for primitive arrays, is that Arrays.asList when given an int[ ] returns a List<Integer[ ]> rather than a List<Integer>.

Guava has an answer to this in the Ints class. Is has an asList method that will take an int[ ] and return a List<Integer>

Update

int[] a = ...;
int[] b = ...;
List<Integer> aList = Ints.asList(a);
List<Integer> bList = Ints.asList(b);

The above will allow your code to work properly for int[ ] as it works for Integer[ ].

Check out Ints API

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

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;
}

How to make a new List in Java

List myList = new ArrayList();

or with generics (Java 7 or later)

List<MyType> myList = new ArrayList<>();

or with generics (Old java versions)

List<MyType> myList = new ArrayList<MyType>();


Related Topics



Leave a reply



Submit