Create a List of primitive int?

后端 未结 10 1929
夕颜
夕颜 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:15

    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 to IntList like this:

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

    Note: I am a contributor to Eclipse Collections.

提交回复
热议问题