Java Reference
In-Depth Information
Collection<T>
Interface
Package:
java.util
Ancestor interfaces: none
All the exception classes mentioned are unchecked exceptions, which means they are not
required to be caught in a
catch
block or declared in a
throws
clause.
All the exception classes mentioned are in the package
java.lang
and so do not require any
import
statement.
CONSTRUCTORS
Although not offi cially required by the interface, any class that implements the
Collection<T>
interface should have at least two constructors: a no-argument constructor that creates an
empty
Collection<T>
object, and a constructor with one parameter of type
Collection<?
extends T>
that creates a
Collection<T>
object with the same elements as the constructor
argument. The interface does not specify whether the copy produced by the one-argument
constructor is a shallow copy or a deep copy of its argument.
public boolean
contains(Object target)
Returns
true
if the calling object contains at least one instance of
target
. Uses
target.
equals
to determine if
target
is in the calling object.
Throws:
ClassCastException
if the type of
target
is incompatible with the calling object (optional).
NullPointerException
if
target
is
null
and the calling object does not support
null
elements (optional).
public boolean
containsAll(Collection<?> collectionOfTargets)
Returns
true
if the calling object contains all of the elements in
collectionOfTargets
. For
element
in
collectionOfTargets
, this method uses
element.equals
to determine if
element
is in the calling object.
Throws:
ClassCastException
if the types of one or more elements in
collectionOfTargets
are
incompatible with the calling object (optional).
NullPointerException
if
collectionOfTargets
contains one or more
null
elements
and the calling object does not support
null
elements (optional).
NullPointerException
if
collectionOfTargets
is
null
.
public boolean
equals(Object other)
This is the
equals
of the collection, not the
equals
of the elements in the collection. Overrides
the inherited method
equals
. Although there are no offi cial constraints on
equals
for a
collection, it should be defi ned as we have described in Chapter 7 and also satisfy the intuitive
notion of collections being equal.