Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Check out three collections libraries

Alternatives and additions to the Java Collections Framework

  • Print
  • Feedback

Page 3 of 4

These elements form the basic framework. By design, the framework is not thread-safe. Safe simultaneous access from multiple threads requires you to wrap the collection with a thread-safe wrapper. Similar wrappers exist for read-only versions of the collections.

While the Collections Framework is relatively small, it is not meant to provide support for every data structure needed. Instead, it is only the framework from which you can build more specialized implementations.

The Jakarta Commons Collections component

Many specialized implementations are well defined and understood, but they are not part of the core Collections Framework yet. Several of these implementations might be included with the concurrency utilities library (JSR 166) discussed in more detail later.

The Jakarta Commons Collections component is one example of a set of specialized implementations. Designed to work with J2SE 1.2, the Commons Collections component provides two List implementations and eight Map implementations. New base structures are also available and are the most interesting.

Let's look at the custom implementations in groups.

The simplest to explain are the FastArrayList, FastHashMap, and FastTreeMap implementations. These extend the standard ArrayList, HashMap, and TreeMap implementations that offer safe simultaneous read-write access from multiple threads. However, safety comes at the cost of slower write operations. While the read operations aren't synchronized, the write operations are performed on a data clone before the existing structure is replaced.

The Bag acts like a Set but permits duplicates. Two more implementations are available here: HashBag and TreeBag; the latter is sorted.

Another new interface is PriorityQueue. It supports comparable items and the use of a Comparator, so items can be maintained in a BinaryHeap based on priority and subsequently removed from the heap based on that priority.

The remaining implementations are a set of specialized Map implementations. They offer special-purpose, key-value pair lookup maps.

  • BeanMap uses reflection to provide key-value pairs based on JavaBean properties; the key is the property name, and the value is its value.
  • ExtendedProperties provides an extension to the java.util.Properties class that concatenates multiple values for a single property, instead of overwriting it.
  • LRUMap is a Map that lets you maintain a maximum capacity and uses a least-used (accessed) algorithm to determine which node to remove when full. This capability is already accessible from the standard LinkedHashMap class, but is only available with J2SE 1.4
  • .

  • SoftRefHashMap works like WeakHashMap but uses SoftReference instead of WeakReference for its keys.
  • MultiHashMap provides a multimap, where keys can have multiple associated values. Fetching the value for a key returns a Collection instead of the specific value for the key.
  • DoubleOrderedMap is the most interesting of the bunch. It provides a Map that supports fast lookup by value and by key. It has one requirement though: both keys and values must be unique. You can do this with two TreeMap objects, but DoubleOrderedMap only saves each key-value pair once.


Other classes and interfaces serve mostly supporting roles, but some specialized behavior is available. Besides utility methods to work with sets like isSubCollection and union, the framework includes additional Comparators and Iterators. The Comparators are used mostly for chaining or changing another Comparator's behavior. For instance, while java.util.Collections.reverseOrder() lets you reverse the natural order of elements, the ReverseComparator class lets you reverse the order from any Comparator. The Iterators support the process of passing each Collection element to a function (method) before getting the element back. This is a shared behavior of JGL and STL too.

  • Print
  • Feedback

Resources