Java Reference
In-Depth Information
COLLECTION CLASSES
The classes in
java.util
that support collections that are sets, lists, queues, and maps include the following:
Collection Classes That Are Sets
•
HashSet<T>
: Implements a set using
HashMap<T>
under the covers. Although a set is by definition
unordered, there has to be some way to find an object reasonably efficiently. Using a
HashMap<T>
object to implement the set enables store and retrieve operations to be done in a constant time.
However, the order in which elements of the set are retrieved might vary over time.
•
LinkedHashSet<T>
: Implements a set using a hash table with all the entries in a doubly-linked
list. This class can be used to make a copy of any set such that iteration ordering is preserved —
something that does not apply to a
HashSet<>
.
•
TreeSet<T>
: Implements a set that orders the objects in ascending sequence. This means that an
iterator obtained from a
TreeSet<T>
object provides the objects in ascending sequence. You can
also search the set for the closest match to a given item.
•
EnumSet<E extends Enum<E>>
: Implements a specialized set that stores enum values from a
single enum type,
E
.
Collection Classes That Are Lists
•
Vector<T>
: Implements a list as an array that automatically increases in size to accommodate as
many elements as you need. Objects are stored and retrieved using an integer index. You can also
use an iterator to retrieve objects from a
Vector<>
. The
Vector<>
type is synchronized — that is,
it is well behaved when concurrently accessed by two or more threads. I will discuss threads and
synchronization in Chapter 16.
•
ArrayList<T>
: Implements an array that can vary in size and can also be accessed as a linked list.
This provides a similar function to the
Vector<T>
generic type but is unsynchronized, so it is not
safe for use by multiple threads.
•
Stack<T>
: This class is derived from
Vector<T>
and adds methods to implement a stack — a last-
in, first-out storage mechanism.
•
LinkedList<T>>:
Implements a linked list. The linked list defined by this class can also be used
as a stack or a queue.
Collection Classes That Are Queues
•
ArrayDeque<T>
: Implements a resizable array as a double-ended queue.
•
PriorityQueue<T>>:
Implements a priority queue in which objects are ordered in ascending se-
quence from the head of the queue. The order is determined either by a
Comparator<T>
object
supplied to the constructor for the collection class, or through the
compareTo()
method declared
in the
Comparable<T>
interface that the object type implements.
Collection Classes That Are Maps
•
Hashtable<K,V>
: Implements a map with keys of type
K
and values of type
V
where all keys must
be non-
null
. The key class must implement the
hashcode()
and
equals()
methods to work ef-
fectively. This type, like
Vector<T>
, is synchronized, so it's safe for concurrent use by two or
more threads.
•
HashMap<K,V>
: Implements a map where objects of type
V
are stored using keys of type
K
. This
collection allows
null
objects to be stored and allows a key to be
null
(only one of course, be-
cause keys must be unique). The map is not synchronized.