Java Reference
In-Depth Information
Figure 8-1. Collections classes and inheritance
SortedSet
and
SortedMap
are specialized sets and maps that maintain their ele‐
ments in a sorted order.
Collection
,
Set
,
List
,
Map
,
SortedSet
, and
SortedMap
are all interfaces, but the
java.util
package also defines various concrete implementations, such as lists
based on arrays and linked lists, and maps and sets based on hash tables or binary
trees. Other important interfaces are
Iterator
and
Iterable
, which allow you to
loop through the objects in a collection, as we will see later on.
The Collection Interface
Collection<E>
is a parameterized interface that represents a generalized grouping
of objects of type
E
. Methods are defined for adding and removing objects from the
group, testing an object for membership in the group, and iterating through all ele‐
ments in the group. Additional methods return the elements of the group as an
array and return the size of the collection.
The grouping within a
Collection
may or may not allow
duplicate elements and may or may not impose an ordering
on the elements.
The Java Collections Framework provides
Collection
because it defines the fea‐
tures common to all common forms of data structure. The JDK ships
Set
,
List
, and
Queue
as subinterfaces of
Collection
. The following code illustrates the operations
you can perform on
Collection
objects:
// Create some collections to work with.
Collection
<
String
>
c
=
new
HashSet
<>();
// An empty set