Java Reference
In-Depth Information
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. A new array should be
returned so that the calling object has no references to the returned array.
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 described in the table for
the
Collection <T>
interface.
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.
ADDING AND REMOVING ELEMENTS
Although many are optional, the following methods are almost always implemented for classes
that implement the
Set <T>
interface.
public boolean
add(T element)
(Optional)
If
element
is not already in the calling object,
element
is added to the calling object and
true
is
returned. If
element
is in the calling object, the calling object is unchanged and
false
is returned.
Throws:
UnsupportedOperationException
if the
add
method is not supported by the set.
ClassCastException
if the class of
element
prevents it from being added to the set.
NullPointerException
if
element
is
null
and the set does not support
null
elements.
IllegalArgumentException
if some other aspect of
element
prevents it from being added
to this set.
public boolean
addAll(Collection<?
extends
T> collectionToAdd)
(Optional)
Ensures that the calling object contains all the elements in
collectionToAdd.
Returns
true
if the
calling object changed as a result of the call; returns
false
otherwise. Thus, if
collectionToAdd
is
a
Set <T>
, then the calling object is changed to the union of itself with
collectionToAdd.
Throws:
UnsupportedOperationException
if the
addAll
method is not supported by the set.
ClassCastException
if the class of some element of
collectionToAdd
prevents it from
being added to the calling object.
NullPointerException
if
collectionToAdd
contains one or more
null
elements and the
calling object does not support
null
elements, or if
collectionToAdd
is
null.
IllegalArgumentException
if some aspect of some element of
collectionToAdd
prevents it from being added to the calling object.