The java.util package also provides several useful concrete implementations of these interfaces that will
suffice for most of your needs. For example:
HashSet<E> A Set implemented as a hashtable. A good, general-purpose implementation for
which searching, adding, and removing are mostly insensitive to the size of the contents.
•
TReeSet<E> A SortedSet implemented as a balanced binary tree. Slower to search or modify
than a HashSet, but keeps the elements sorted.
•
ArrayList<E> A List implemented using a resizable array. It is expensive to add or delete an
element near the beginning if the list is large, but relatively cheap to create and fast for random
access.
•
LinkedList<E> A doubly linked List and Queue implementation. Modification is cheap at any
size, but random access is slow.
•
HashMap<K,V> A hashtable implementation of Map. A very generally useful collection with
relatively cheap lookup and insertion times.
•
treeMap<K,V> An implementation of SortedMap as a balanced binary tree to keep its elements
ordered by key. Useful for ordered data sets that require moderately quick lookup by key.
•
WeakHashMap<K,V> An hashtable implementation of Map that references its keys with weak
reference objects (see page 454). This is useful only in limited situations.
•
Nearly all the implementation classes in java.util are both Cloneable and Serializable. The
exceptions are PriorityQueue which is not Cloneable, and WeakHashMap which is neither.
In this chapter you first learn about iteration because it is useful with all the collection classes. We then cover
ordering since it is used by many of the collection types. We then present the details of the
Collection-based types, followed by the Map-based types. After that we look at the various utilities
available, and we show you how to make unmodifiable and type-safe versions of your collections. Next we
show you the synchronized and concurrent collections. You will then learn how to write your own iteration
and collection types in case you have a need that the provided classes do not fill. Finally, we cover the "legacy
collections" in java.util that predate the overall collection system, but which you will find in some
existing code. One of these legacy collection typesPropertiescontinues in common use.
For simplicity, we usually refer to the various interfaces without mentioning their type parametersfor example,
Iterator or Collectionexcept where it is necessary to show that the same type parameter is being
usedfor example, that iterator has a return type Iterator
21.1.1. Exception Conventions
A few conventions related to exceptions are used throughout the collections classes and interfaces, and we do
not wish to document them for every constructor and method where they may occur:
Methods that are optional in an implementation of an interface throw
UnsupportedOperationException when not implemented. We indicate which methods are
optional as we go throughusing "(Optional)" at the end of the method description.
•
Methods or constructors that accept elements (either individually or as part of other collections) to be
added to the current collection can throw ClassCastException if the element is not of an
appropriate type for the collection, or for sorted collections, if the element cannot be compared with
other elements. Methods that check for the existence of elements or try to remove them may also
throw ClassCastException.
•
Methods or constructors that accept elements (either individually or as part of other collections) to be
added to the current collection throw IllegalArgumentException if the element's value is not
appropriate for the collectionfor example, some collections, such as subsets, define restricted ranges
on the values of elements allowed in the collection.
•
- Methods that return individual elements of a collection will give you a