The Java Collections API is a set of classes and interfaces designed to store multiple objects.
There are a variety of classes that store objects in different ways:
The basic distinctions are defined in several interfaces in the
Collectionis the most basic generally useful interface that most, but not all, collection classes implement.
Iteratorthat can be used to loop through all elements of the collection (more on this later).
Iterableinterface, which only requires one method, which supplies an object used to iterate through the collection.
Listinterface adds the ability to insert and delete at a specified index within the collection, or retrieve from a specific position.
Setinterface expects that implementing classes will modify the add methods to prevent duplicates, and also that any constructors will prevent duplicates (the
addmethod returns a
booleanthat states whether the operation succeeded or not; i.e., if the object could be added because it was not already there).
Mapinterface does not extend
Collection, because its adding, removing, and retrieving methods use a key value to identify an element.
put(Object key, Object value),
In addition to the
Map categories, there are also several inferences you can make from some of the collection class names:
Comparableinterface implemented by many classes including
Stringand the numeric wrapper classes) or an order dictated by a special helper class object (a
Several additional interfaces are used to define useful helper classes:
Enumerationprovides a pair of methods that enable you to loop through a collection in a standardized manner, regardless of the type of collection (you test if there are more elements, and, if so, retrieve the next element).
Iteratorconcept and were specified to use enumerations, so they will still appear in new code.
Iteratoris an improved version that allows an object to be removed from the collection through the iterator.
The following diagrams show some of the classes and interfaces that are available: