Why another collections library?

The Java libraries included basic Hashtable, Stack and Vector collections classes from the beginning. With Java 2, the addition of the Collections Library increased the variety of collections dramatically, as well as offering improved performance in some areas. Even with the improved support in Java 2, though, there's still reason to use alternatives to the standard library classes for high-performance applications.

Performance and clarity benefits

The biggest performance problem with the standard classes is that they only work with objects, not with primitive types. To build a collection of primitive values, such as ints, you need to create an object wrapper for each value added to the collection. This causes considerable overhead both when initially adding values and when accessing the values later, since you have to first cast the object reference to the appropriate type and then retrieve the actual value.

Even with collections of objects, the library classes are not optimized for performance. Timing measurements listed under the Timings topic show a better than 20 percent increase in performance using the classes from this library in a test with Integer objects and generic collections. For primitive int values in a type-specific collection the performance increase is much greater, to almost 4 times that for the Integers in a standard IntArray.

In addition, these library classes allow more robust code because no casting of values is required. Casting bypasses the type checking done by the compiler and defers the checking to runtime, when mistakes are generally a lot more painful to catch. Using type-specific collections restores the type checking responsibility to the compiler and assures that errors are caught before they're turned into executing code. Getting rid of the casts also makes your code cleaner and easier to understand.

Why not to use these classes

The biggest drawback to type-specific collections is that Java requires tailoring of the code to each type. This library is designed to minimize the effort needed to construct a collection for a type, and comes with unit tests which can easily be adapted to new type-specific variations of the included collections. If you're working with types which aren't included in the library you're still going to need to do extra work to build the collection classes you want, though.

If your code is heavily using collections of some particular type the performance and robustness gains may be worth the effort. Before doing this, though, consider if it's possible to use a plain array (the simplest form of type-specific collection!) for your needs. If you can use an array you'll have the best of both worlds - code that offers top performance along with simplicity and ease of use.

License

To make it as easy as possible to reuse this code, it has been released as open source code under the liberal MIT License (our specific license is available here).