24

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.

oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
Daniel Bingham
  • 12,414
  • 18
  • 67
  • 93

5 Answers5

37

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<Integer> l = new ArrayList<Integer>();
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<String, Integer> m = new HashMap<String, Integer>();
m.put("Hello", 5);
int i = m.get("Helo Misspelt"); //will throw a NullPointerException
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
  • 2
    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
  • 13
    + 1 for the remove(4) remove(new Integer(4)) example! –  Jul 25 '10 at 01:01
  • 2
    **Primitive Collections for Java** has been flagged as deleted on SourceForge. – Stephan Sep 11 '13 at 14:29
  • @oxbow_lakes why does l.remove throws a error? What is the reason behind this? – Ayushi Jain Apr 02 '18 at 18:51
  • @ayushi it's because List has two remove() methods. [`remove(int)`](https://docs.oracle.com/javase/8/docs/api/java/util/List.html#remove-int-) and [`remove(E)`](https://docs.oracle.com/javase/8/docs/api/java/util/List.html#remove-java.lang.Object-)... `l.remove(4)` says "remove the fourth element of the list". Whereas, `l.remove(new Integer(4))` says "remove the first occurrence of '4' in the list wherever that is". Check out the linked java docs for more detailed info. – Pradeep Gollakota Apr 06 '18 at 07:41
  • @PradeepGollakota Thanks for the answer.. Recently, I got a detailed answer over [here](https://stackoverflow.com/questions/49617334/why-no-autoboxing-while-removing-primitive-type-from-a-list-in-java) – Ayushi Jain Apr 06 '18 at 17:51
  • Just to make things clear, the `NullPointerException` thrown in line `int i = m.get("Helo Misspelt");` is not because of the `get()` method cannot find the key in the `Map`, but because of the auto-unboxing which happens while trying to assign the `Integer` value to primitive `int`. `Integer i = m.get("Helo Misspelt");` will not throw a `NullPointerException`. – Roshana Pitigala Aug 22 '19 at 17:33
13

http://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.

(LGPL license)

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 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
6

Modern Java supports autoboxing of primitives, so you can say

List<Integer> lst = new ArrayList<Integer>;
lst.add(42);

That at least avoids the syntactic vinegar of new Integer(42).

Thom Smith
  • 13,916
  • 6
  • 45
  • 91
  • 3
    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 20 '09 at 04:20
  • I've never heard the term syntactic vinegar before. Can't wait to use that. Thanks! – delrocco Jan 30 '20 at 21:09
5

Joda-Primitives.

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

MikeFHay
  • 8,562
  • 4
  • 31
  • 52
cherouvim
  • 31,725
  • 15
  • 104
  • 153
2

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.

Donald Raab
  • 6,458
  • 2
  • 36
  • 44