|
Collections Framework Enhancements
|
This page summarizes enhancements to the collections framework in Java SE 6.
This release saw fewer API changes than 5.0, but there was more of
a focus on the accuracy and clarity of the specification. We
recommend using the Java SE 6 specification even when writing programs
for older releases.
The primary theme of the API changes was better bi-directional
collection access.
These new collection interfaces are provided:
- Deque
- a double ended queue, supporting element insertion and
removal at both ends. Extends the Queue interface.
- BlockingDeque
- a Deque with operations that wait for the deque to become
non-empty when retrieving an element, and wait for space to become
available in the deque when storing an element. Extends both the
Deque and BlockingQueue interfaces.
(This interface is part of java.util.concurrent.)
- NavigableSet
- a SortedSet extended with navigation methods reporting
closest matches for given search targets. A NavigableSet may be
accessed and traversed in either ascending or descending order.
This interface is intended to supersede the SortedSet interface.
- NavigableMap
- a SortedMap extended with navigation methods returning the
closest matches for given search targets. A NavigableMap may
be accessed and traversed in either ascending or descending key order.
This interface is intended to supersede the SortedMap interface.
- ConcurrentNavigableMap
- a ConcurrentMap that is also a NavigableMap.
(This interface is part of java.util.concurrent.)
The following concrete implementation classes have been added:
These existing classes have been retrofitted to implement new interfaces:
- LinkedList -
retrofitted to implement the Deque interface.
- TreeSet -
retrofitted to implement the NavigableSet interface.
- TreeMap -
retrofitted to implement the NavigableMap interface.
Two new methods were added to the Collections utility class:
- newSetFromMap(Map)
- creates a general purpose Set implementation from a general
purpose Map implementation.
There is no IdentityHashSet class, but instead, just use
Set<Object> identityHashSet=
Collections.newSetFromMap(
new IdentityHashMap<Object, Boolean>());
- asLifoQueue(Deque)
- returns a view of a Deque as a Last-in-first-out (Lifo) Queue.
The Arrays utility class now has methods
copyOf
and copyOfRange
that can efficiently resize, truncate, or copy subarrays for arrays of all types.
Before:
int[] newArray = new int[newLength];
System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
After:
int[] newArray = Arrays.copyOf(a, newLength);