130

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?

Augustus
  • 1,479
  • 2
  • 17
  • 31
Susie
  • 5,038
  • 10
  • 53
  • 74
  • If performance is your concern, trove should help. – assylias Aug 02 '13 at 18:55
  • It's possible to create a list using a basic array, but you do have to know the size of the array to start with, and you'll lose a lot of other functionality that comes with the Collections object. But you could do something like: int[] array = new int[10]; – Taylor Aug 12 '14 at 23:09
  • long needs 8Bytes, Long needs 8Bytes plus 16Bytes for Object header (in 64bit system). It's a shame, there is no primitive list. Large data sets should always be hold as primitives... – kaiser Jan 22 '21 at 10:35

12 Answers12

174

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
Doomjunky
  • 1,148
  • 17
  • 18
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • 1
    So the JTable method setSelectedIndices is a non-sense : https://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html#setSelectedIndices-int%3AA- – vdolez Jul 23 '16 at 07:22
  • `List` might lead to devastating memory fragmentation. Java maintains constant pool for some integers in 0..128 range but generally Java allocates a new object for each 32-bit integer (wasting at least 16-4=12 bytes of RAM) + worsening GC performance. – gavenkoa Jan 19 '23 at 16:20
29

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.

Mike B
  • 1,522
  • 1
  • 14
  • 24
  • 2
    I couldn't find a primitive collection implementation in Guava. Can you provide a link please? – nimcap Dec 16 '14 at 15:30
9

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);
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
7

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

stolen_leaves
  • 1,441
  • 11
  • 19
5

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.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
5

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

List<Integer> list = new ArrayList<>();
Reimeus
  • 158,255
  • 15
  • 216
  • 276
5

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.

Nikhil Nanivadekar
  • 1,152
  • 11
  • 10
1

JEP 402 from project Valhalla, promises to allow this. They will remove the need for wrapper classes altogether, which means no more boxing and unboxing.

..so watch out for that

Borislav Stoilov
  • 3,247
  • 2
  • 21
  • 46
0

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.

Cristik
  • 30,989
  • 25
  • 91
  • 127
Robin Davies
  • 7,547
  • 1
  • 35
  • 50
0

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

Stefan Medack
  • 2,731
  • 26
  • 32
  • That's a strong statement. Certainly `SparseIntArray` is frequently a better option, but there are tradeoffs to be aware of. "The implementation is not intended to be appropriate for data structures that may contain large numbers of items. It is generally slower than a traditional HashMap..." – Andy Thomas Dec 08 '15 at 16:58
  • Yes, that's true. They cover this tradeoff pretty good in the video I linked. But the OP asked for that particular possibility and I just wanted to add that it is actually possible with Android and also recommended for most use cases. Thanks for adding that information! – Stefan Medack Dec 08 '15 at 21:50
0

If you have an array of primitive ints, you can convert it into a list using java streams.

List<Integer> lst = Arrays.stream(arr).boxed().collect(Collectors.toList());

when reading the values from list you can use intValue() to get it as primitive int

lst.get(0).intValue();

-5

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)

Selvaraj
  • 714
  • 2
  • 6
  • 14