[java] 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?

This question is related to java list generics collections

The answer is


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.


You can use primitive collections available in Eclipse Collections. Eclipse Collections has List, Set, Bag and Map for all primitives. The elements in the primitive collections are maintained as primitives and no boxing takes place.

You can initialize a IntList like this:

MutableIntList ints = IntLists.mutable.empty();

You can convert from a List<Integer> to IntList like this:

List<Integer> integers = new ArrayList<>();
MutableIntList ints = ListAdapter.adapt(integers).collectInt(each -> each);

Note: I am a contributor to Eclipse Collections.


This is not possible. The java specification forbids the use of primitives in generics. However, you can create ArrayList<Integer> and call add(i) if i is an int thanks to boxing.


When you use Java for Android development, it is recommended to use SparseIntArray to prevent autoboxing between int and Integer.

You can finde more information to SparseIntArray in the Android Developers documentation and a good explanation for autoboxing on Android enter link description here


Java Collection should be collections of Object only.

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

Integer is wrapper class of primitive data type int.

more from JAVA wrapper classes here!

U can directly save and get int to/from integerList as, integerList.add(intValue); int intValue = integerList.get(i)


Is there a way to convert an Integer[] array to an int[] array?

This gross omission from the Java core libraries seems to come up on pretty much every project I ever work on. And as convenient as the Trove library might be, I am unable to parse the precise requirements to meet LPGL for an Android app that statically links an LGPL library (preamble says ok, body does not seem to say the same). And it's just plain inconvenient to go rip-and-stripping Apache sources to get these classes. There has to be a better way.


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


Collections use generics which support either reference types or wilcards. You can however use an Integer wrapper

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

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

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to list

Convert List to Pandas Dataframe Column Python find elements in one list that are not in the other Sorting a list with stream.sorted() in Java Python Loop: List Index Out of Range How to combine two lists in R How do I multiply each element in a list by a number? Save a list to a .txt file The most efficient way to remove first N elements in a list? TypeError: list indices must be integers or slices, not str Parse JSON String into List<string>

Examples related to generics

Instantiating a generic type Are these methods thread safe? The given key was not present in the dictionary. Which key? Using Java generics for JPA findAll() query with WHERE clause Using Spring RestTemplate in generic method with generic parameter How to create a generic array? Create a List of primitive int? How to have Java method return generic list of any type? Create a new object from type parameter in generic class What is the "proper" way to cast Hibernate Query.list() to List<Type>?

Examples related to collections

Kotlin's List missing "add", "remove", Map missing "put", etc? How to unset (remove) a collection element after fetching it? How can I get a List from some class properties with Java 8 Stream? Java 8 stream map to list of keys sorted by values How to convert String into Hashmap in java How can I turn a List of Lists into a List in Java 8? MongoDB Show all contents from all collections Get nth character of a string in Swift programming language Java 8 Distinct by property Is there a typescript List<> and/or Map<> class/library?