The primary advantages of a collections framework are thatit:
The collections framework consists of:
Thecollection interfaces are divided into two groups.The most basic interface,java.util.Collection,has the following descendants:
The other collection interfaces are based onjava.util.Map and arenot true collections. However, these interfaces containcollection-view operations, which enable them to bemanipulated as collections.Map has the followingoffspring:
Many of the modification methods in the collection interfacesare labeledoptional. Implementations are permitted to notperform one or more of these operations, throwing a runtimeexception (UnsupportedOperationException) if they areattempted. The documentation for each implementation must specifywhich optional operations are supported. Several terms areintroduced to aid in this specification:
Some implementations restrict what elements (or in the case ofMaps, keys and values) can be stored. Possiblerestrictions include requiring elements to:
Attempting to add an element that violates an implementation'srestrictions results in a runtime exception, typically aClassCastException, anIllegalArgumentException,or aNullPointerException. Attempting to remove or testfor the presence of an element that violates an implementation'srestrictions can result in an exception. Some restrictedcollections permit this usage.
Classes that implement the collection interfaces typically havenames in the form of<Implementation-style><Interface>.The general purpose implementations are summarized in the followingtable:
Interface | Hash Table | Resizable Array | Balanced Tree | Linked List | Hash Table + Linked List |
---|---|---|---|---|---|
Set | HashSet | TreeSet | LinkedHashSet | ||
List | ArrayList | LinkedList | |||
Deque | ArrayDeque | LinkedList | |||
Map | HashMap | TreeMap | LinkedHashMap |
The general-purpose implementations support all of theoptional operations in the collection interfaces and have norestrictions on the elements they may contain. They areunsynchronized, but theCollections class contains staticfactories calledsynchronization wrappers that can be used to addsynchronization to many unsynchronized collections. All of the newimplementations havefail-fast iterators, which detectinvalid concurrent modification, and fail quickly and cleanly(rather than behaving erratically).
TheAbstractCollection,AbstractSet,AbstractList,AbstractSequentialList andAbstractMap classes provide basic implementations of thecore collection interfaces, to minimize the effort required toimplement them. The API documentation for these classes describesprecisely how each method is implemented so the implementer knowswhich methods must be overridden, given the performance of thebasic operations of a specific implementation.
Applications that use collections from more than one thread mustbe carefully programmed. In general, this is known asconcurrentprogramming. The Java platform includes extensive support forconcurrent programming. SeeJava ConcurrencyUtilities for details.
Collections are so frequently used that various concurrentfriendly interfaces and implementations of collections are includedin the APIs. These types go beyond the synchronization wrappersdiscussed previously to provide features that are frequently neededin concurrent programming.
These concurrent-aware interfaces are available:
The following concurrent-aware implementation classes areavailable. See the API documentation for the correct usage of theseimplementations.
The main design goal was to produce an API that was small insize and, more importantly, in "conceptual weight." Itwas critical that the new functionality not seem too different tocurrent Java programmers; it had to augment current facilities,rather than replace them. At the same time, the new API had to bepowerful enough to provide all the advantages describedpreviously.
To keep the number of core interfaces small, the interfaces donot attempt to capture such subtle distinctions as mutability,modifiability, and resizability. Instead, certain calls in the coreinterfaces areoptional, enabling implementations to throwanUnsupportedOperationException to indicate that they donot support a specified optional operation. Collection implementersmust clearly document which optional operations are supported by animplementation.
To keep the number of methods in each core interface small, aninterface contains a method only if either:
It was critical that all reasonable representations ofcollections interoperate well. This included arrays, which cannotbe made to implement theCollection interface directlywithout changing the language. Thus, the framework includes methodsto enable collections to be moved into arrays, arrays to be viewedas collections, and maps to be viewed as collections.