Java Reference
In-Depth Information
Display 16.2
Method Headings in the
Collection<T>
Interface
(part 2 of 3)
public
boolean
containsAll(Collection<?> collectionOfTargets)
Returns
true
if the calling object contains all of the elements in
collectionOfTargets
. For an
element in
collectionOfTargets
, this method uses
element.equals
to determine if
element
is in the calling object.
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 official constraints on
equals
for a collec-
tion, it should be defined as we have described in Chapter 7 and also to satisfy the intuitive notion
of collections being equal.
public
int
size( )
Returns the number of elements in the calling object. If the calling object contains more than
Integer.MAX_VALUE
elements, returns
Integer.MAX_VALUE
.
Iterator<T> iterator( )
Returns an iterator for the calling object. (Iterators are discussed in Section 16.3.)
public
Object[ ] toArray( )
Returns an array containing all of the elements in the calling object. If the calling object makes any
guarantees as to what order its elements are returned by its iterator, this method must return the
elements in the same order.
The array returned should be a new array so that the calling object has no references to the
returned array. (You might also want the elements in the array to be clones of the elements in the
collection. However, this is apparently not required by the interface, since library classes, such as
Vector<T>
, return arrays that contain references to the elements in the collection.)
public
<E> E[ ] toArray(E[ ] a)
Note that the type parameter
E
is not the same as
T
. So,
E
can be any reference type; it need not
be the type
T
in
Collection<T>
. For example,
E
might be an ancestor type of
T
.
Returns an array containing all of the elements in the calling object. The argument
a
is used prima-
rily to specify the type of the array returned. The exact details are as follows:
The type of the returned array is that of
a
. If the elements in the calling object fit in the array
a
,
then
a
is used to hold the elements of the returned array; otherwise a new array is created with
the same type as
a
.
If
a
has more elements than the calling object, the element in
a
immediately following the end of
the copied elements is set to
null
.
If the calling object makes any guarantees as to what order its elements are returned by its iterator,
this method must return the elements in the same order. (Iterators are discussed in Section 16.3.)
public
int
hashCode( )
Returns the hash code value for the calling object.