Java Reference
In-Depth Information
public int
hashCode()
Returns the hash code value for the calling object. Neither hash codes nor this method are
discussed in this topic. This entry is only here to make the defi nition of the
Collection<T>
interface complete. You can safely ignore this entry until you go on to study hash codes in a more
advanced book. In the meantime, if you need to implement this method, have the method throw
an
UnsupportedOperationException
.
public boolean
isEmpty()
Returns
true
if the calling object is empty; otherwise returns
false
.
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, because 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
primarily 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 fi t 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.)
Throws:
ArrayStoreException
if the type of
a
is not an ancestor type of the type of every element in
the calling object.
NullPointerException
if
a
is
null
.
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
.
OPTIONAL METHODS
The following methods are optional, which means they still must be implemented,
but the implementation can simply throw an
UnsupportedOperationException
if for some reason you do not want to give them a “real” implementation. An
UnsupportedOperationException
is a
RunTimeException
and so is not required to be
caught or declared in a
throws
clause.