Create a List of primitive int?

后端 未结 10 1924
夕颜
夕颜 2020-11-27 12:43

Is there a way to create a list of primitive int or any primitives in java like following?

List myList = new ArrayList();
10条回答
  •  醉梦人生
    2020-11-27 13:09

    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.

提交回复
热议问题