Outline of the Collections Framework
The collections framework consists of:- Collection interfaces - The primary means by
which collections are manipulated.
- Collection - A group of objects. No assumptions are made about the order of the collection (if any) or whether it can contain duplicate elements.
- Set - The
familiar set abstraction. No duplicate elements permitted. May or
may not be ordered. Extends the
Collectioninterface. - List -
Ordered collection, also known as a sequence. Duplicates are
generally permitted. Allows positional access. Extends the
Collectioninterface. - Queue - A
collection designed for holding elements before processing. Besides
basic
Collectionoperations, queues provide additional insertion, extraction, and inspection operations. - Deque - A
double ended queue, supporting element insertion and
removal at both ends. Extends the
Queueinterface. - Map - A mapping from keys to values. Each key can map to one value.
- SortedSet
- A set whose elements are automatically sorted, either in their
natural ordering (see the
Comparableinterface) or by aComparatorobject provided when aSortedSetinstance is created. Extends theSetinterface. - SortedMap
- A map whose mappings are automatically sorted by key, either
using the natural ordering of the keys or by a comparator
provided when a
SortedMapinstance is created. Extends theMapinterface. - NavigableSet
- A
SortedSetextended with navigation methods reporting closest matches for given search targets. ANavigableSetmay be accessed and traversed in either ascending or descending order. - NavigableMap
- A
SortedMapextended with navigation methods returning the closest matches for given search targets. ANavigableMapcan be accessed and traversed in either ascending or descending key order. - BlockingQueue
- A
Queuewith operations that wait for the queue to become nonempty when retrieving an element and that wait for space to become available in the queue when storing an element. (This interface is part of thejava.util.concurrentpackage.) - TransferQueue
- A
BlockingQueuein which producers can wait for consumers to receive elements. (This interface is part of thejava.util.concurrentpackage.) - BlockingDeque
- A
Dequewith operations that wait for the deque to become nonempty when retrieving an element and wait for space to become available in the deque when storing an element. Extends both theDequeandBlockingQueueinterfaces. (This interface is part of thejava.util.concurrentpackage.) - ConcurrentMap
- A
Mapwith atomicputIfAbsent,remove, andreplacemethods. (This interface is part of thejava.util.concurrentpackage.) -
ConcurrentNavigableMap - A
ConcurrentMapthat is also aNavigableMap.
- General-purpose implementations - The primary
implementations of the collection interfaces.
- HashSet - Hash
table implementation of the
Setinterface. The best all-around implementation of theSetinterface. - TreeSet
- Red-black tree implementation of the
NavigableSetinterface. - LinkedHashSet
- Hash table and linked list implementation of the
Setinterface. An insertion-orderedSetimplementation that runs nearly as fast asHashSet. - ArrayList -
Resizable array implementation of the
Listinterface (an unsynchronizedVector). The best all-around implementation of theListinterface. - ArrayDeque -
Efficient, resizable array implementation of the
Dequeinterface. - LinkedList
- Doubly-linked list implementation of the
Listinterface. Provides better performance than theArrayListimplementation if elements are frequently inserted or deleted within the list. Also implements theDequeinterface. When accessed through theQueueinterface,LinkedListacts as a FIFO queue. - PriorityQueue - Heap implementation of an unbounded priority queue.
- HashMap - Hash
table implementation of the
Mapinterface (an unsynchronizedHashtablethat supportsnullkeys and values). The best all-around implementation of theMapinterface. - TreeMap
Red-black tree implementation of the
NavigableMapinterface. - LinkedHashMap
- Hash table and linked list implementation of the
Mapinterface. An insertion-orderedMapimplementation that runs nearly as fast asHashMap. Also useful for building caches (see removeEldestEntry(Map.Entry) ).
- HashSet - Hash
table implementation of the
- Wrapper implementations -
Functionality-enhancing implementations for use with other
implementations. Accessed solely through static factory methods.
-
Collections.unmodifiableInterface -
Returns an unmodifiable view of a specified collection that throws
an
UnsupportedOperationExceptionif the user attempts to modify it. - Collections.synchronizedInterface - Returns a synchronized collection that is backed by the specified (typically unsynchronized) collection. As long as all accesses to the backing collection are through the returned collection, thread safety is guaranteed.
-
Collections.checkedInterface - Returns
a dynamically type-safe view of the specified collection, which
throws a
ClassCastExceptionif 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 bypass this mechanism. Dynamically type-safe views eliminate this possibility.
-
Collections.unmodifiableInterface -
Returns an unmodifiable view of a specified collection that throws
an
- Adapter implementations - Implementations that
adapt one collections interface to another:
-
newSetFromMap(Map) - Creates a general-purpose
Setimplementation from a general-purposeMapimplementation. -
asLifoQueue(Deque) - Returns a view of a
Dequeas a Last In First Out (LIFO)Queue.
-
newSetFromMap(Map) - Creates a general-purpose
- Convenience implementations - High-performance
"mini-implementations" of the collection interfaces.
- Arrays.asList - Enables an array to be viewed as a list.
- emptySet, emptyList and emptyMap - Return an immutable empty set, list, or map.
- singleton, singletonList, and singletonMap - Return an immutable singleton set, list, or map, containing only the specified object (or key-value mapping).
- nCopies - Returns an immutable list consisting of n copies of a specified object.
- Legacy implementations - Older collection classes were retrofitted to implement the collection interfaces.
- Special-purpose implementations
- WeakHashMap
- An implementation of the
Mapinterface that stores only weak references to its keys. Storing only weak references enables key-value pairs to be garbage collected when the key is no longer referenced outside of theWeakHashMap. This class is the easiest way to use the power of weak references. It is useful for implementing registry-like data structures, where the utility of an entry vanishes when its key is no longer reachable by any thread. - IdentityHashMap
- Identity-based
Mapimplementation based on a hash table. This class is useful for topology-preserving object graph transformations (such as serialization or deep copying). To perform these transformations, you must 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 preventing "spoof attacks" resulting from intentionally perverse equals methods. (IdentityHashMapnever invokes the equals method on its keys.) An added benefit of this implementation is that it is fast. - CopyOnWriteArrayList
- A
Listimplementation backed by an copy-on-write array. All mutative operations (such asadd,set, andremove) are implemented by making a new copy of the array. No synchronization is necessary, even during iteration, and iterators are guaranteed never to throwConcurrentModificationException. This implementation is well-suited to maintaining event-handler lists (where change is infrequent, and traversal is frequent and potentially time-consuming). - CopyOnWriteArraySet
- A
Setimplementation backed by a copy-on-write array. This implementation is similar toCopyOnWriteArrayList. Unlike mostSetimplementations, theadd,remove, andcontainsmethods require time proportional to the size of the set. This implementation is well suited to maintaining event-handler lists that must prevent duplicates. - EnumSet - A
high-performance
Setimplementation backed by a bit vector. All elements of eachEnumSetinstance must be elements of a single enum type. - EnumMap - A
high-performance
Mapimplementation backed by an array. All keys in eachEnumMapinstance must be elements of a single enum type.
- WeakHashMap
- An implementation of the
- Concurrent implementations - These
implementations are part of
java.util.concurrent.- ConcurrentLinkedQueue - An unbounded first in, first out (FIFO) queue based on linked nodes.
- LinkedBlockingQueue - An optionally bounded FIFO blocking queue backed by linked nodes.
- ArrayBlockingQueue - A bounded FIFO blocking queue backed by an array.
- PriorityBlockingQueue - An unbounded blocking priority queue backed by a priority heap.
- DelayQueue - A time-based scheduling queue backed by a priority heap.
- SynchronousQueue
- A simple rendezvous mechanism that uses the
BlockingQueueinterface. - LinkedBlockingDeque - An optionally bounded FIFO blocking deque backed by linked nodes.
-
LinkedTransferQueue - An unbounded
TransferQueuebacked by linked nodes. - ConcurrentHashMap
- A highly concurrent, high-performance
ConcurrentMapimplementation based on a hash table. This implementation never blocks when performing retrievals and enables the client to select the concurrency level for updates. It is intended as a drop-in replacement forHashtable. In addition to implementingConcurrentMap, it supports all of the legacy methods ofHashtable. -
ConcurrentSkipListSet - Skips list implementation of
the
NavigableSetinterface. -
ConcurrentSkipListMap - Skips list implementation of
the
ConcurrentNavigableMapinterface.
- Abstract implementations - Skeletal
implementations of the collection interfaces to facilitate custom
implementations.
- AbstractCollection
- Skeletal
Collectionimplementation that is neither a set nor a list (such as a "bag" or multiset). - AbstractSet
- Skeletal
Setimplementation. - AbstractList
- Skeletal
Listimplementation backed by a random access data store (such as an array). - AbstractSequentialList
- Skeletal
Listimplementation backed by a sequential access data store (such as a linked list). - AbstractQueue
- Skeletal
Queueimplementation. - AbstractMap
- Skeletal
Mapimplementation.
- AbstractCollection
- Skeletal
- Algorithms - The Collections
class contains these useful static methods.
- sort(List) - Sorts a list using a merge sort algorithm, which provides average case performance comparable to a high quality quicksort, guaranteed O(n*log n) performance (unlike quicksort), and stability (unlike quicksort). A stable sort is one that does not reorder equal elements.
- binarySearch(List, Object) - Searches for an element in an ordered list using the binary search algorithm.
- reverse(List) - Reverses the order of the elements in a list.
- shuffle(List) - Randomly changes the order of the elements in a list.
- fill(List, Object) - Overwrites every element in a list with the specified value.
- copy(List dest, List src) - Copies the source list into the destination list.
- min(Collection) - Returns the minimum element in a collection.
- max(Collection) - Returns the maximum element in a collection.
- rotate(List list, int distance) - Rotates all of the elements in the list by the specified distance.
- replaceAll(List list, Object oldVal, Object newVal) - Replaces all occurrences of one specified value with another.
- indexOfSubList(List source, List target) - Returns the index of the first sublist of source that is equal to target.
- lastIndexOfSubList(List source, List target) - Returns the index of the last sublist of source that is equal to target.
- swap(List, int, int) - Swaps the elements at the specified positions in the specified list.
- frequency(Collection, Object) - Counts the number of times the specified element occurs in the specified collection.
- disjoint(Collection, Collection) - Determines whether two collections are disjoint, in other words, whether they contain no elements in common.
- addAll(Collection<? super T>, T...) - Adds all of the elements in the specified array to the specified collection.
- Infrastructure
- Iterators - Similar to the familiar Enumeration
interface, but more powerful, and with improved method names.
- Iterator
- In addition to the functionality of the
Enumerationinterface, enables the user to remove elements from the backing collection with well-defined, useful semantics. - ListIterator
- Iterator for use with lists. In addition to the functionality of
the
Iteratorinterface, supports bidirectional iteration, element replacement, element insertion, and index retrieval.
- Iterator
- In addition to the functionality of the
- Ordering
- Comparable - Imparts a natural ordering to classes that implement it. The natural ordering can be used to sort a list or maintain order in a sorted set or map. Many classes were retrofitted to implement this interface.
- Comparator
- Represents an order relation, which can be used to sort a list or
maintain order in a sorted set or map. Can override a type's
natural ordering or order objects of a type that does not implement
the
Comparableinterface.
- Runtime exceptions
- UnsupportedOperationException - Thrown by collections if an unsupported optional operation is called.
- ConcurrentModificationException - Thrown by iterators and list iterators if the backing collection is changed unexpectedly while the iteration is in progress. Also thrown by sublist views of lists if the backing list is changed unexpectedly.
- Performance
- RandomAccess
- Marker interface that lets
Listimplementations indicate that they support fast (generally constant time) random access. This lets generic algorithms change their behavior to provide good performance when applied to either random or sequential access lists.
- RandomAccess
- Marker interface that lets
- Iterators - Similar to the familiar Enumeration
interface, but more powerful, and with improved method names.
- Array Utilities
- Arrays -
Contains static methods to sort, search, compare, hash, copy,
resize, convert to
String, and fill arrays of primitives and objects.
- Arrays -
Contains static methods to sort, search, compare, hash, copy,
resize, convert to
Copyright © 1998, 2017, Oracle and/or its affiliates. 500 Oracle Parkway
Redwood Shores, CA 94065 USA. All rights reserved.