Java Vector or ArrayList for Primitives?

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: List l = new ArrayList(); l.

Add(4); l. Remove(4); //will throw ArrayIndexOutOfBoundsException l. Remove(new Integer(4)); //what you probably intended!

And it is also a common source of mysterious NullPointerExceptions accessing (perhaps via a Map ): Map m = new HashMap(); m. Put(" 5); int I = m. Get("Helo Misspelt"); //will throw a NullPointerException.

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: List l = new ArrayList(); l. Add(4); l. Remove(4); //will throw ArrayIndexOutOfBoundsException l.

Remove(new Integer(4)); //what you probably intended! And it is also a common source of mysterious NullPointerExceptions accessing (perhaps via a Map): Map m = new HashMap(); m. Put(" 5); int I = m.

Get("Helo Misspelt"); //will throw a NullPointerException.

1 So in other words, use one of the third party libraries or write your own. Got it, thanks! :) – Daniel Bingham Aug 19 '09 at 18:22 1 + 1 for the remove(4) remove(new Integer(4)) example!

– functional Jul 25 '10 at 1:01.

trove4j.sourceforge.net/ The Trove library provides high speed regular and primitive collections for Java. Note that because Trove uses primitives, the types it defines do not implement the java. Util collections interfaces.

This is for commercial software development. While I think we're okay to use LGPL'd code I'd have to check with people, and in that case it'd probably be easier to just write my own class. I'll make note of the library for future Open Source stuff I write though, thanks!

– Daniel Bingham Aug 19 '09 at 18:09 If LGPL is off-limits, that rules out the a very large proportion of open-source libraries. What were you expecting? – skaffman Aug 19 '09 at 18:12 Just needed to know whether or not I was missing something in the JDK.

LGPL isn't off limits, but I can write my own class in this case in less time than it would take to get the okay on the library, get it integrated and then write the code using it. – Daniel Bingham Aug 19 '09 at 18:14.

Modern Java supports autoboxing of primitives, so you can say List lst = new ArrayList; lst. Add(42); That at least avoids the syntactic vinegar of new Integer(42).

2 This is dangerous (reasons given below) – oxbow_lakes Aug 19 '09 at 18:10 Indeed -- some methods of Java collections are overloaded, and when autoboxing is involved, Java may resolve invocations that are conceptually ambiguous by selecting the non-autoboxed option rather than generating a compile-time error. Conceptually, the reason for this is that int and Integer are isomorphic, but there exists no subtype relationship between them. This kind of relationship exists nowhere else in Java but autoboxing (and a few esoteric issues with generics and type erasure).

– Thom Smith Aug 19 '09 at 19:08 2 new Integer(42) is the wrong thing to do, use Integer. ValueOf(42) for boxing. – starblue Aug 19 '09 at 20:09 The nice thing about autoboxing is that I don't have to remember that.

;-) – Thom Smith Aug 19 '09 at 4:20 1 +1 for introducing me to the phrase "syntactic vinegar" – Don Aug 19 '09 at 0:05.

Joda-Primitives. There is also Primitive Collections for Java but it's a bit out of date.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions