The main advantage of the primitive collections is that they are signficantly smaller than their java.util equivalents. How much smaller?
Object-based Collection | Bytes per Element | Primitive Collection | Bytes per Element | Space Savings |
---|---|---|---|---|
ArrayList of Bytes | 16 | ArrayByteList | 1 | 93.4% |
ArrayList of Shorts | 16 | ArrayShortList | 2 | 87.5% |
ArrayList of Characters | 16 | ArrayCharList | 4 | 75.0% |
ArrayList of Floats | 16 | ArrayFloatList | 4 | 75.0% |
ArrayList of Integers | 16 | ArrayIntist | 4 | 75.0% |
ArrayList of Doubles | 16 | ArrayDoubleList | 8 | 50.0% |
ArrayList of Longs | 16 | ArrayLongList | 8 | 50.0% |
Depending upon your circumstances, you may also find the primitive collections to be faster, both because of the reduction in time spent boxing and un-boxing the primitives and their Object wrappers, and because there are fewer bytes to move around when copying or moving data.
Finally, in the pre-autoboxing (JDK 1.5) world, you may also find the primitive collections easier to work with, since you'll waste fewer keystrokes manually boxing and un-boxing the primitives and their Object wrappers. For instance, to sum an ArrayList of Integers, you might write something like:
int sum = 0; for(Iterator iter = list.iterator(); iter.hasNext(); ) { sum += ((Integer)(iter.next())).intValue(); }
The IntList equivalent would be:
int sum = 0; for(IntIterator iter = list.iterator(); iter.hasNext(); ) { sum += iter.next(); }
Yes.
The primitive collections are most useful for applications that create very many or very large collections of primitive types, or that process them very frequently.
No.
Using generics, one can create collections that are specific to a particular Object from a templated implementation, for instance, a List of Integers or a List of Strings from the same prototype implemenation. Since the distinction between Java primitives and Java Objects is not going away, it will not be possible to, for example, instantiate a List of ints from that same prototype.
Using autoboxing, it is possible to emulate the syntax of using the primitive collections. For example, list.add(19) will work equally well whether list is a List or IntList, since the compiler will automatically convert list.add(19) into list.add(new Integer(19)).
While this sugar-coating of the primitive-to-Object-wrapper syntax is a welcome improvement, it does not change the underlying representation of the List. That is, ArrayList will still use 16 bytes per element, even if that element could be represented by a single byte.