Java Reference
In-Depth Information
21.4. The
Collection
Interface
As you have seen, most collection types are subtypes of the
Collection
interface. The only exceptions are those that are subtypes of
Map
. In
this section we cover the
Collection
interface. The following sections de-
scribe the interfaces that extend
Collection
and the specific collection im-
plementations provided in
java.util
. We leave
Map
and its related types
for later. We also defer talking about implementing your own collection
types.
The basis of much of the collection system is the
Collection
interface. As
you saw in
Figure 21-1
, most of the actual collection types implement
this interface, usually by implementing an extended interface such as
Set
or
List
. So
Collection
is a good place to start understanding collections.
It has the following primary methods for working with an individual col-
lection:
public int
size()
Returns the size of this collection, that is, the number of ele-
ments it currently holds. The value returned is limited to
In-
teger.MAX_VALUE
even if the collection holds more elements.
public boolean
isEmpty()
Returns
TRue
if this collection currently holds no elements.
public boolean
contains(Object elem)
Returns
true
if the collection contains the object
elem
; that is,
if this collection has an element on which invoking
equals
with
elem
returns
true
. If
elem
is
null
, returns
TRue
if there is a
null
element in the collection.
public Iterator<E>
iterator()