Is there an expandable array class in the Java API equivalent to the Vector
or ArrayList
class that can be used with primitives (int, char, double, etc)?
I need a quick, expandable array for integers and it seems wasteful to have to wrap them in the Integer
class in order to use them with Vector
or ArrayList
. My google-fu is failing me.
Modern Java supports autoboxing of primitives, so you can say
That at least avoids the syntactic vinegar of new Integer(42).
http://trove4j.sourceforge.net/
Note that because Trove uses primitives, the types it defines do not implement the java.util collections interfaces.
(LGPL license)
There is unfortunately no such class, at least in the Java API. There is the Primitive Collections for Java 3rd-party product.
It's pretty dangerous to use auto-boxing together with existing collection classes (in particular
List
implementations). For example:And it is also a common source of mysterious
NullPointerExceptions
accessing (perhaps via aMap
):Joda-Primitives.
There is also Primitive Collections for Java but it's a bit out of date.
Eclipse Collections has primitive ArrayLists for all primitive types, as well as primitive Sets, Bags, Stacks and Maps. There are immutable versions of all of the primitive container types as well.
Note: I am a committer for Eclipse Collections.