This page summarizes enhancements to the collections
framework in version 5 of the JDK.
Collections Framework Enhancements
Three new language features significantly enhance the collections framework:
Three new collection interfaces are provided:
- Generics - adds compile-time type safety to the collections
framework and eliminates the need to cast when reading elements from
- Enhanced for loop - eliminates the need for explicit
iterators when interating over collections.
- Autoboxing/unboxing - automatically converts primitives (such as
int) to wrapper classes (such as Integer) when inserting
them into collections, and converts wrapper class instances to primitives when
reading from collections.
Two new concrete Queue
implementations are provided, one existing List implementation has
been retrofitted to implement Queue, and one abstract Queue
implementation is provided:
- Queue - represents
a collection designed for holding elements prior to processing.
Besides basic Collection operations, queues provide
additional insertion, extraction, and inspection operations.
- extends Queue with operations that wait for the queue to become
non-empty when retrieving an element, and that wait for space to become
available in the queue when storing an element. (This interface is part of
the new package java.util.concurrent.)
- extends Map
with atomic putIfAbsent, remove, and replace
methods. (This interface is part of java.util.concurrent.)
Five new BlockingQueue
implementations are provided, all of which are part of
One ConcurrentMap implementation is provided:
- PriorityQueue - an unbounded priority queue backed by a heap.
- an unbounded thread-safe FIFO (first-in first-out) queue backed by linked
nodes. (This class is part of java.util.concurrent.)
- LinkedList -
retrofitted to implement the Queue interface. When accessed via the
Queue interface, LinkedList behaves as a FIFO queue.
- AbstractQueue - a skeletal Queue implementation.
are provided for use in situations where read operations vastly outnumber
write operations and iteration cannot or should not be synchronized:
- a highly concurrent, high-performance ConcurrentMap implementation backed by a hash table. This implementation never blocks when performing retrievals and allows the client to select the concurrency level for updates. It is intended as a drop-in replacement for Hashtable: in addition to implementing ConcurrentMap, it supports all of the "legacy" methods peculiar to Hashtable.
Map implementations are
provided for use with enums:
- a List implementation backed by an array. All mutative operations
(such as add, set, and remove) are implemented by
making a new copy of the array. No synchronization is necessary, even during
iteration, and iterators are guaranteed never to throw
ConcurrentModificationException. This implementation is well-suited
to maintaining event-handler lists (where change is infrequent, and traversal
is frequent and potentially time-consuming).
- a Set implementation backed by a copy-on-write array. This
implementation is similar in nature to CopyOnWriteArrayList. Unlike
most Set implementations, the add, remove,
and contains methods require time proportional to the size of the
set. This implementation is well-suited to maintaining event-handler lists
that must prevent duplicates.
A new family of wrapper implementations is provided, primarily for use
with generic collections:
- a high-performance Set implementation backed by a bit-vector. All
elements of each EnumSet instance must be elements of a single enum
- a high-performance Map implementation backed by an array. All
keys in each EnumMap instance must be elements of a single enum
Three new generic algorithms and one comparator converter were
added to the Collections utility class:
The Arrays utility class has been outfitted with content-based
methods for arrays of all types. These methods complement the existing
Versions of all three methods are provided to operate on nested (or
deepToString. It is now trivial to print the contents of any array.
The idiom for printing a "flat" array is:
- returns a dynamically typesafe view of the specified collection, which
throws a ClassCastException if a client attempts to add an element of
the wrong type. The generics mechanism in the language provides compile-time
(static) type checking, but it is possible to defeat this mechanism.
Dynamically typesafe views eliminate this possibility entirely.
The idiom for printing a nested (multidimensional) array is:
was retrofitted to implement Comparable.