Create a List of primitive int?
Is there a way to create a list of primitive int or any primitives in java like following?
List<int> myList = new ArrayList<int>();
It seems I can do List myList = new ArrayList();
and add "int" into this list. But then this would mean I can add anything into this list.
Is my only option, creating an array of int and converting it into a list or creating a list of Integer objects?
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
No there isn't any collection that can contain primitive types when Java Collection Framework is being used.
However, there are other java collections which support primitive types, such as: Trove, Colt, Fastutil, Guava
An example of how an arraylist with ints would be when Trove Library used is the following:
TIntArrayList list= new TIntArrayList();
The performance of this list, when compared with the ArrayList of Integers from Java Collections is much better as the autoboxing/unboxing to the corresponding Integer Wrapper Class is not needed.
Is there a way to create a list of primitive int or any primitives in java
No you can't. You can only create List of reference types, like Integer
, String
, or your custom type.
It seems I can do
List myList = new ArrayList();
and add "int" into this list.
When you add int
to this list, it is automatically boxed to Integer
wrapper type. But it is a bad idea to use raw type lists, or for any generic type for that matter, in newer code.
I can add anything into this list.
Of course, that is the dis-advantage of using raw type. You can have Cat, Dog, Tiger, Dinosaur, all in one container.
Is my only option, creating an array of int and converting it into a list
In that case also, you will get a List<Integer>
only. There is no way you can create List<int>
or any primitives.
You shouldn't be bothered anyways. Even in List<Integer>
you can add an int
primitive types. It will be automatically boxed, as in below example:
List<Integer> list = new ArrayList<Integer>();
list.add(5);
Try using the ArrayIntList from the apache framework. It works exactly like an arraylist, except it can hold primitive int.
More details here -
https://commons.apache.org/dormant/commons-primitives/apidocs/org/apache/commons/collections/primitives/ArrayIntList.html