Java does not let us create ArrayList
of primitives directly. But we can create a ArrayList
of the primitives wrapper class.
But when accessing the primitive value , compiler automatically does the wrapping and unwrapping. Is this not an overhead ? My question is what are performance implications of this ?
In such a case wouldn't int[]
perform better than ArrayList<Integer>
?
Has anyone measured performance of int[]
vs ArrayList<Inetger>
for Java5.0
(when this was first introduced) and latter versions of Java. It would be really helpful for everybody who runs into this question , if you could share the test results.
Indeed it adds some overhead. If it is a problem for you, consider using some library supporting collections of primitives out of the box. For example Trove, Guava or google for another library