Chapter 16. Efficient Containers

Table of Contents

Class CombinedCollection
Design Considerations
Primitive Containers
Available Operations And Their Performance Costs
Arraylist Usage Examples
Fast Multiple Value Lookups
ArrayList Size And Capacity
Iterators

The Java® SDK has included the Collections API since version 1.2. This API is found in the java.util package and consists of container implementations for linked-lists, growing arrays, hashtables and balanced trees. See the Collections Guide at the SunCol website for detailed documentation and examples.

The aforementioned containers are limited to holding reference datatypes, which includes instances of classes derived from java.lang.Object. The only support you will find for arrays of primitive data types is in the Arrays utility class SunArr. However, this support is limited, which is why we have provided a specialized ArrayList container for primitive types. This class can be found in package com.tensegrity.generic.util. [7]

Class CombinedCollection

In addition to supporting primitive types, the Tensegrity API provides a class called CombinedCollection, which allows nested combined collection views that are constructed in linear time.

Class CombinedCollection merges two collections into a new collection view consisting of the contents of both. Because the underlying collections are not physically combined, the cost of creating the combined List is linear.

An instance does not allow modification to any of its contents, but changes to the underlying collections are immediately reflected. The sole exception is during an iteration. In other words, it is possible to iterate over a CombinedCollection and change the underlying lists without having a ConcurrentModificationException being thrown.

It is not possible to alter the combined collection instance or their contents directly, however.

Unsupported operations will cause a RuntimeException to be thrown, which indicates that the user is trying something defined by the Collection interface yet which contradicts the semantics of the read-only view that a CombinedCollection provides.

The cost of creating a new CombinedCollection instance is very low and allows quick pseudo-concatenation of two existing Collection instances. The resulting object can be traversed with an Iterator and its size can also be determined. Additionally, the instance can be passed to constructors of other standard Collection objects, since its toObject method has been appropriately overridden. The number of operations that are supported by this class are limited, however, and restricted to read-only operations.

Example 16.1. CombinedCollection Usage

List al = new ArrayList ();
al.add ("one");
al.add ("two");
List ll = new LinkedList ();
ll.add ("three");
ll.add ("four");
two list instances were created, now a combined view of this
lists consisting of the elements "one", "two", "three", "four"
will be created by means of CombinedCollection
  
Collection collection = new CombinedCollection (al, ll);
iterating over the collection will now yield
all four inserted elements from both input collections.



[7] As a general rule of thumb, a standard API implementation is more likely to be higher in quality than your own code. Sometimes, however, it pays to create a new wheel if more efficiency is gained in those parts of the code that are likely to be executed often.