An idea: auto-unboxing
Primitive CollectionsI'm aware of at least two libraries for Java that provide collections for each of the six primitive types. So instead of storing Integer Objects in an ArrayList, you get to store int primitives in an IntArrayList. This saves RAM and it speeds things up. It's a clever optimization.
But this optimization is not very Java-ish. Where's the reusable in reusable collections when we need seven of them? It is a terrible violation of the Don't Repeat Yourself mantra.
Autoboxing
Java 5 introduced a new feature for using primitive types with old-school Object collections. It's called autoboxing and basically it allows you to replace this:
List myList = new ArrayList();
myList.add(new Integer(867));
with this:
List myList = new ArrayList();
myList.add(867);
This is simple syntactic sugar. The compiler implicitly adds the
new Integer()
for you.Idea: Auto-unboxing
What if Java supported primitives with generics? This could be implemented in the compiler: ArrayList<T> could be converted into all of List<int>, List<short> etc. This would require seven .class files, which is already necessary for the other primitive collections implementations. But it would require only a single .java file.
Some problems
Collections classes use some methods only found in Object.class such as
hashCode
, equals()
etc. This would need fixing with some unnatural hackery. The compiler would also need to know when to compile such a primitive generic class.