| Documentation Contents

# Collections Framework Enhancements

This page summarizes the enhancements made to the collections framework in version 1.4 of the JavaTM 2 SDK.

- The `Collections` utility class has several new methods:
  - `rotate(List list, int distance)` - Rotates all of the elements in the list by the the specified distance. This operation is common in GUI computations: moving one or more columns in a table can be accomplished efficiently by applying this operation to a sublist. This operation is also common in mathematical and scientific calculations.

  - `replaceAll(List list, Object oldVal, Object newVal)` - Replaces all occurrences of one specified value with another. This is essentially a convenience method. Though not difficult to program, it's very commonly needed.

  - `indexOfSubList(List source, List target)` - Returns the index of the first sublist of `source` that is equal to `target`. Commonly used in many domains including text processing.

  - `lastIndexOfSubList(List source, List target)` - Returns the index of the last sublist of `source` that is equal to `target`. Commonly used in many domains including text processing.

  - `swap(List list, int i, int j)` - Swaps the elements at the specified positions in the specified list. Essentially a convenience method, though this implementation is faster than the naive implementation.

  - `list(Enumeration e)` - Returns an `ArrayList` containing the elements returned by the specified enumeration. This convenience method provides interoperability between legacy APIs that return enumerations and new APIs that require collections.

- New interface `RandomAccess` is a marker interface that allows `List` implementations to indicate that they support fast (generally constant time) random access. This allows generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.

- New class `LinkedHashMap` provides an insertion-ordered `Map` implementation that runs nearly as fast as `HashMap`. Internally, it uses a hash table with a doubly linked list running through it in insertion order. Also available is a corresponding `Set` implementation, called `LinkedHashSet`.

- New class `IdentityHashMap` is an identity-based Map implementation based on a hash table. This class is useful for topology-preserving object graph transformations (such as serialization or deep-copying). To perform such transformations, you need to maintain an identity-based "node table" that keeps track of which objects have already been seen. Identity-based maps are also used to maintain object-to-meta-information mappings in dynamic debuggers and similar systems. Finally, identity-based maps are useful in thwarting "spoof attacks" resulting from intentionally perverse equals methods. (`IdentityHashMap` never invokes the equals method on its keys.) An added benefit of this implementation is that it is fast.

Copyright © 1995-2010 Sun Microsystems, Inc. All Rights Reserved. Please send comments using this Feedback page. | Java Technology