Collections Framework Enhancements
This page summarizes enhancements to the collections framework in
version 5 of the JDK.
Three new language features significantly enhance the
- Generics - adds compile-time type safety to the
collections framework and eliminates the need to cast when reading
elements from collections.
- 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
Three new collection interfaces are provided:
- represents a collection designed for holding elements prior to
processing. Besides basic Collection operations, queues
provide additional insertion, extraction, and inspection
- 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
Two new concrete Queue
are provided, one existing List
implementation has been
retrofitted to implement Queue
, and one abstract
implementation is provided:
- 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
- LinkedList -
retrofitted to implement the Queue interface. When
accessed via the Queue interface, LinkedList
behaves as a FIFO queue.
- a skeletal Queue implementation.
Five new BlockingQueue
implementations are provided, all of which are part of
implementation is provided:
- 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.
provided for use in situations where read operations vastly
outnumber write operations and iteration cannot or should not be
- 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
- 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.
provided for use with enums:
- EnumSet - a
high-performance Set implementation backed by a
bit-vector. All elements of each EnumSet instance must be
elements of a single enum type.
- EnumMap - a
high-performance Map implementation backed by an array.
All keys in each EnumMap instance must be elements of a
single enum type.
A new family of wrapper implementations is provided, primarily for
use with generic collections:
Collections.checkedInterface - 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
Three new generic algorithms and one comparator converter were
added to the Collections
utility class has been outfitted with
methods for arrays of all types. These methods complement the
methods. Versions of all three methods are provided
to operate on nested (or "multidimensional") arrays:
. It is now trivial to print the contents of
any array. The idiom for printing a "flat" array is:
The idiom for printing a nested (multidimensional) array is:
Finally, Boolean was
retrofitted to implement Comparable.