The collections framework consists of:
Annotated Outline of Collections Framework
- 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 may contain duplicate elements.
- Set - The familiar set abstraction. No duplicate elements permitted. May or may not be ordered. Extends the Collection interface.
- List - Ordered collection, also known as a sequence. Duplicates are generally permitted. Allows positional access. Extends the Collection interface.
- Map - A mapping from keys to values. Each key can map to at most one value.
- SortedSet - A set whose elements are automatically sorted, either in their natural ordering (see the Comparable interface), or by a Comparator object provided when a SortedSet instance is created. Extends the Set interface.
- SortedMap - A map whose mappings are automatically sorted by key, either in the keys' natural ordering or by a comparator provided when a SortedMap instance is created. Extends the Map interface.
- General-Purpose Implementations - The primary implementations of the collection interfaces.
- HashSet - Hash table implementation of the Set interface. The best all-around implementation of the Set interface.
- TreeSet Red-black tree implementation of the SortedSet interface.
- LinkedHashSet - Hash table and linked list implementation of the Set interface. An insertion-ordered Set implementation that runs nearly as fast as HashSet.
- ArrayList - Resizable-array implementation of the List interface. (Essentially an unsynchronized Vector.) The best all-around implementation of the List interface.
- LinkedList - Doubly-linked list implementation of the List interface. May provide better performance than the ArrayList implementation if elements are frequently inserted or deleted within the list. Useful for queues and double-ended queues (deques).
- HashMap - Hash table implementation of the Map interface. (Essentially an unsynchronized Hashtable that supports null keys and values.) The best all-around implementation of the Map interface.
- TreeMap Red-black tree implementation of the SortedMap interface.
- LinkedHashMap - Hash table and linked list implementation of the Map interface. An insertion-ordered Map implementation that runs nearly as fast as HashMap. Also useful for building caches (see java.util.Map.Entry)">removeEldestEntry(Map.Entry) ).
- Wrapper Implementations - Functionality-enhancing implementations for use with other implementations. Accessed soley through static factory methods.
- java.util.Collection)">Collections.unmodifiableInterface - Return an unmodifiable view of a specified collection that throws an UnsupportedOperationException if the user attempts to modify it.
- java.util.Collection)"> Collections.synchronizedInterface - Return 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.
- Convenience Implementations - High-performance "mini-implementations" of the collection interfaces.
- Arrays.asList - Allows an array to be viewed as a list.
- EMPTY_SET, EMPTY_LIST and EMPTY_MAP - Constants representing the empty set and list (immutable).
- java.lang.Object)">singleton, java.lang.Object)">singletonList, and singletonMap - Returns 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 have been retrofitted to implement the collection interfaces.
- Special Purpose Implementations
- WeakHashMap - an implementation of the Map interface that stores only weak references to its keys. Storing only weak references allows key-value pairs to be garbage-collected when the key is no longer referenced outside of the WeakHashMap. This class provides the easiest way to harness 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 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.
Abstract Implementations - Partial implementations of the collection interfaces to facilitate custom implementations.
- AbstractCollection - Skeletal implementation of a collection that is neither a set nor a list (such as a "bag" or multiset).
- AbstractSet - Skeletal implementation of a set.
- AbstractList - Skeletal implementation of a list backed by a random-access data store (such as an array).
- AbstractSequentialList - Skeletal implementation of a list backed by a sequential-access data store (such as a linked list).
- AbstractMap - Skeletal implementation of a map.
Algorithms
- java.util.List)"> 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.
- java.util.List)">reverse(List) - Reverses the order of the elements in the a list.
- java.util.List)">shuffle(List) - Randomly permutes the elements in a list.
- fill(List, Object) - Overwrites every element in a list with the specified value.
- java.util.List)">copy List(dest, List src) - Copies the source list into the destination list.
- java.util.Collection)"> min(Collection) - Returns the minimum element in a collection.
- java.util.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 the specified distance.
- replaceAll List(list, Object oldVal, Object newVal) - Replaces all occurrences of one specified value with another.
- java.util.List)">indexOfSubList List(source, List target) - Returns the index of the first sublist of source that is equal to target.
- java.util.List)">lastIndexOfSubList List(source, List target) - Returns the index of the last sublist of source that is equal to target.
- swap List(list, int, int) - Swaps the elements at the specified positions in the specified list.
Infrastructure
- Iterators - Similar to the familiar Enumeration interface, but more powerful, and with improved method names.
- Iterator - In addition to the functionality of the Enumeration interface, allows 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 Iterator interface, supports bi-directional iteration, element replacement, element insertion and index retrieval.
- Ordering
- Comparable - Imparts a natural ordering to classes that implement it. The natural ordering may be used to sort a list or maintain order in a sorted set or map. Many classes have been retrofitted to implement this interface.
- Comparator - Represents an order relation, which may 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 Comparable interface.
- 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 modified unexpectedly while the iteration is in progress. Also thrown by sublist views of lists if the backing list is modified unexpectedly.
- Performance
- RandomAccess - 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.
Array Utilities
- Arrays - Contains static methods to sort, search, compare and fill arrays of primitives and Objects.