Java Reference
In-Depth Information
These are not all the interfaces defined in the
java.util
package, just the ones that relate to the topics
in this chapter. You can see that the interfaces for maps have no connection to the interfaces implemented
by sets and lists. You can also see that the map interfaces do not implement the
Iterable<T>
interface, so
you cannot use the collection-based
for
loop to iterate over the objects in a map. However, the
Map<K,V>
interface declares a
values()
method that returns a collection view of the objects in a map as type
Collec-
tion<V>
. You can use the
Collection<V>
reference with a collection-based
for
loop to access the contents
of the map because the
Collection<V>
type extends
Iterable<V>
.
There are four basic collection interfaces — the
Set<T>
,
List<T>
,
Queue<T>
, and
Map<K,V>
interfaces —
that relate to the fundamental organization of objects in a collection. The first three inherit the members of
Iterable<T>
and
Collection<T>
, so sets, lists, and queues share the characteristics specified by these two
interfaces.
SortedSet<T>
and
SortedMap<K,V>
are specialized versions of their respective superinterfaces
that add methods for ordering objects within a collection. The
NavigableSet<T>
and
NavigableMap<K,V>
interfaces provide further specialization that defines the capability for finding the closest match for a given
search target in a collection. Don't confuse the
Collection<T>
interface with the
Collections
class (with
an
s
) that you will see later.
These interfaces are implemented by the classes from the
java.util
package that I have introduced, as
shown in
Table 14-1
:
IMPLEMENTED BY
Set<T> HashSet<T>, LinkedHashSet<T>, EnumSet<T>
SortedSet<T> TreeSet<T>
List<T> Vector<T>, Stack<T>, ArrayList<T>, LinkedList<T>
Queue<T> PriorityQueue<T>, LinkedList<T>
Deque<T> LinkedList<T>, ArrayDeque<T>
Map<K,V> Hashtable<K,V>, HashMap<K,V>, LinkedHashMap<K,V>, WeakHashMap<K,V>, Iden-
tityHashMap<K,V>, EnumMap<K extends Enum<K>,V>
SortedMap<K,V> TreeMap<K,V>
NavigableMap<K,V>TreeMap<K,V>
The
LinkedList<T>
type implements the
List<T>
, the
Queue<T>
, and the
Deque<T>
interfaces, so it
really does have multiple personalities in that you can regard a
LinkedList<T>
object as a linked list, as a
queue, or as a double-ended queue.
Keep in mind that any collection class object that implements the
Collection<T>
interface can be ref-
erenced using a variable of type
Collection<T>
. This means that any of the list or set collections can be
referenced in this way; only the map class types are excluded (but not entirely, as you can obtain a set from
a map, and the classes implementing a map can provide a view of the values stored as a
Collection<T>
reference). You see that using a parameter of type
Collection<T>
is a standard way of passing a list or a set
to a method.
These interfaces involve quite a large number of methods, so rather than go through them in the abstract,
let's see them at work in the context of some specific collection class types.
USING ENUMSET