Interface Description Concrete Classes: Summary of Collection and Map Implementations
Interface Description Concrete Classes: Summary of Collection and Map Implementations
Classes
Collection A basic interface that defines the normal operations that
allow a collection of objects to be maintained or handled
as a single unit.
Set The Set interface extends the Collection interface to HashSet
LinkedHashSet
represent its mathematical namesake: a set of unique
elements.
SortedSet The SortedSet interface extends the Set interface to TreeSet
provide the required functionality for maintaining a set in
which the elements are stored in some sorted order.
List The List interface extends the Collection interface to ArrayList
maintain a sequence of elements that need not be unique. Vector
LinkedList
Map A basic interface that defines operations for maintaining HashMap
Hashtable
mappings of keys to values. LinkedHashMap
SortedMap Extends the Map interface for maps that maintain their TreeMap
mappings sorted in key order.
Basic Operations
The basic operations are used to query a collection about its contents and allow elements
to be added and removed from a collection.
int size()
boolean isEmpty()
boolean contains(Object element)
boolean add(Object element) // Optional
boolean remove(Object element) // Optional
The add() and remove() methods return true if the collection was modified as a result
of the operation.
By returning the value false, the add() method indicates that the collection excludes
duplicates, and that the collection already contains an object equal to the argument object.
The contains() method checks for membership of the argument object in the collection
using object value equality.
Bulk Operations
boolean containsAll(Collection c)
boolean addAll(Collection c) // Optional
boolean removeAll(Collection c) // Optional
boolean retainAll(Collection c) // Optional
void clear() // Optional
These bulk operations can be used to perform the equivalent of set logic on arbitrary
collections (not just on sets). The containsAll() method returns true if all elements of
the specified collection are also contained in the current collection.
The addAll(), removeAll(), and retainAll() methods are destructive in the sense that
the collection on which they are invoked can be modified.
Array Operations
Object[] toArray()
Object[] toArray(Object a[])
The first toArray() method returns an array with all the elements of the collection. The
second method stores the elements of a collection into an array of a specified type.
If the given array is big enough, the elements are stored in this array. If there is room to
spare in the array, that is, the length of the array is greater than the number of elements in
the collection, the spare room is filled with null values before the array is returned. If the
array is too small, a new array of the same runtime type and appropriate size is created. If
the runtime type of the specified array is not a supertype of the runtime type of every
element in the collection, an ArrayStoreException is thrown.
Iterators
Iterator iterator()
boolean hasNext()
Returns true if the underlying collection still has elements left to return. A future call to
the next() method will return the next element from the collection.
Object next()
Moves the iterator to the next element in the underlying collection, and returns the
current element. If there are no more elements left to return, it throws a
NoSuchElementException.
Object remove()
Removes the element that was returned by the last call to the next() method, from the
underlying collection. Invoking this method results in an IllegalStateException, if the
next() method has not yet been called, or when the remove() method has already been
called after the last call to the next() method. This method is optional for an iterator, that
is, it throws an UnsupportedOperationException if the remove operation is not
supported.
After obtaining the iterator for a collection, the methods provided by the Iterator
interface can be used to systematically traverse the elements of the underlying collection
one by one. Example 11.1 illustrates the use of an iterator.
Using an Iterator
The concrete collection classes override the toString() method to provide a textual
representation of their contents. The standard textual representation generated by the
toString() method for a collection is where each elementi is the textual representation
generated by the toString() method of the individual elements in the collection. At (2)
and at (7) the toString() method of the collection class is used implicitly to generate a
textual representation for the collection
The majority of the iterators provided in the java.util package are said to be fail-fast.
When an iterator has already been obtained, structurally modifying the underlying
collection by other means will invalidate the iterator. Subsequent use of this iterator will
throw a ConcurrentModificationException. The remove() method of an iterator is
the only recommended way to delete elements from the underlying collection during
traversal with an iterator.
The order in which the iterator will return the elements from an underlying collection
depends on the traversal order supported by the collection. For example, an iterator for a
list will traverse the elements in the sequential order they have in the list, whereas the
traversal order for the elements in an ordinary set is not predetermined. An iterator for a
sorted collection will make the elements available in a given sorted order.
Sets