2017  Kodetalk | Feedback | Privacy Policy | Terms | About

Why can Java Collections not directly store Primitives types?

Java collections  store the wrapper classe objects but not the primitive types.

Why this constraint?


Makes the implementation easier. Since Java primitives are not considered Objects, you would need to create a separate collection class for each of these primitives (no template code to share).

You can do that, of course, just see GNU TroveApache Commons Primitives or HPPC.

Unless you have really large collections, the overhead for the wrappers does not matter enough for people to care (and when you do have really large primitive collections, you might want to spend the effort to look at using/building a specialized data structure for them).

Answer is