Collection
| Kind of class: | public interface |
|---|---|
| Package: | com.bourre.collection |
| Inherits from: | Iterable |
| Implemented by: | |
| Known subinterfaces: | |
| Author: | Francis Bourre |
| Classpath: | com.bourre.collection.Collection |
| File last modified: | Monday, 24 November 2008, 11:36:49 |
represents a group of objects, known as its elements. Some
collections allow duplicate elements and others do not. Some are ordered
and others unordered. This interface is typically used to pass
collections around and manipulate them where maximum generality is
desired.
The "destructive" methods contained in this interface, that is, the
methods that modify the collection on which they operate, are specified to
throw UnsupportedOperationException if this collection does not
support the operation. If this is the case, these methods may, but are not
required to, throw an UnsupportedOperationException if the
invocation would have no effect on the collection. For example, invoking
the addAll(Collection) method on an unmodifiable collection may,
but is not required to, throw the exception if the collection to be added
is empty.
Some collection implementations have restrictions on the elements that
they may contain. For example, some implementations prohibit null elements,
and some have restrictions on the types of their elements. Attempting to
add an ineligible element throws an unchecked exception, typicallyNullPointerException or ClassCastException. Attempting
to query the presence of an ineligible element may throw an exception,
or it may simply return false; some implementations will exhibit the former
behavior and some will exhibit the latter. More generally, attempting an
operation on an ineligible element whose completion would not result in
the insertion of an ineligible element into the collection may throw an
exception or it may succeed, at the option of the implementation.
Such exceptions are marked as "optional" in the specification for this
interface.
When dealing with typed and untyped collection, the following rules apply :
- Two typed collection, which have the same type, can collaborate each other.
- Two untyped collection can collaborate each other.
- An untyped collection can add, remove, retain or contains any typed collection
of any type without throwing errors. - A typed collection will always fail when attempting to add, remove, retain
or contains an untyped collection.
Summary
- size : uint
- Returns the number of elements in this collection.
- isEmpty : Boolean
- Returns true if this collection contains no elements.
- contains (o:Object) : Boolean
- Returns true if this collection contains the specified
- toArray : Array
- Returns an array containing all of the elements in this collection.
- add (o:Object) : Boolean
- Ensures that this collection contains the specified element (optional
- remove (o:Object) : Boolean
- Removes a single instance of the specified element from this
- containsAll (c:Collection) : Boolean
- Returns true if this collection contains all of the elements
- addAll (c:Collection) : Boolean
- Adds all of the elements in the specified collection to this collection
- removeAll (c:Collection) : Boolean
- Removes all this collection's elements that are also contained in the
- retainAll (c:Collection) : Boolean
- Retains only the elements in this collection that are contained in the
- clear : void
- Removes all of the elements from this collection (optional operation).
Instance methods
add
operation). Returns
true if this collection changed as aresult of the call. (Returns
false if this collection doesnot permit duplicates and already contains the specified element.)
Collections that support this operation may place limitations on what
elements may be added to this collection. In particular, some
collections will refuse to add null elements, and others will
impose restrictions on the type of elements that may be added.
Collection classes should clearly specify in their documentation any
restrictions on what elements may be added.
If a collection refuses to add a particular element for any reason
other than that it already contains the element, it must throw
an exception (rather than returning false). This preserves
the invariant that a collection always contains the specified element
after this call returns.
-
trueif this collection changed as a result of the
call
- {VISDOC_LINK_0}
UnsupportedOperationException—addis not supported by
this collection. - {VISDOC_LINK_1}
ClassCastException— class of the specified element prevents it
from being added to this collection. - {VISDOC_LINK_2}
NullPointerException— if the specified element is null and this
collection does not support null elements. - {VISDOC_LINK_3}
IllegalArgumentException— some aspect of this element prevents
it from being added to this collection.
addAll
(optional operation).
-
trueif this collection changed as a result of the
call
- {VISDOC_LINK_0}
UnsupportedOperationException— if this collection does not
support theaddAllmethod. - {VISDOC_LINK_1}
ClassCastException— if the class of an element of the specified
collection prevents it from being added to this collection. - {VISDOC_LINK_2}
NullPointerException— if the specified collection contains one
or more null elements and this collection does not support null
elements, or if the specified collection isnull. - {VISDOC_LINK_3}
IllegalArgumentException— some aspect of an element of the
specified collection prevents it from being added to this collection.
clear
This collection will be empty after this method returns unless it
throws an exception.
- {VISDOC_LINK_0}
UnsupportedOperationException— if theclearmethod is
not supported by this collection.
contains
true if this collection contains the specifiedelement.
-
trueif this collection contains the specified
element
- {VISDOC_LINK_0}
ClassCastException— if the type of the specified element
is incompatible with this collection (optional). - {VISDOC_LINK_1}
NullPointerException— if the specified element is null and this
collection does not support null elements (optional).
containsAll
true if this collection contains all of the elementsin the specified collection.
-
trueif this collection contains all of the elements
in the specified collection
- {VISDOC_LINK_0}
ClassCastException— if the types of one or more elements
in the specified collection are incompatible with this
collection (optional). - {VISDOC_LINK_1}
NullPointerException— if the specified collection contains one
or more null elements and this collection does not support null
elements (optional). - {VISDOC_LINK_2}
NullPointerException— if the specified collection is
null.
isEmpty
true if this collection contains no elements. -
trueif this collection contains no elements
remove
collection, if this collection contains one or more such
elements. Returns true if this collection contained the specified
element (or equivalently, if this collection changed as a result of the
call).
-
trueif this collection changed as a result of the
call
- {VISDOC_LINK_0}
ClassCastException— if the type of the specified element
is incompatible with this collection (optional). - {VISDOC_LINK_1}
NullPointerException— if the specified element is null and this
collection does not support null elements (optional). - {VISDOC_LINK_2}
UnsupportedOperationException—removeis not supported
by this collection.
removeAll
specified collection (optional operation). After this call returns,
this collection will contain no elements in common with the specified
collection.
-
trueif this collection changed as a result of the
call
- {VISDOC_LINK_0}
UnsupportedOperationException— if theremoveAllmethod
is not supported by this collection. - {VISDOC_LINK_1}
ClassCastException— if the types of one or more elements
in this collection are incompatible with the specified
collection (optional). - {VISDOC_LINK_2}
NullPointerException— if this collection contains one or more
null elements and the specified collection does not support
null elements (optional). - {VISDOC_LINK_3}
NullPointerException— if the specified collection is
null.
retainAll
specified collection (optional operation). In other words, removes from
this collection all of its elements that are not contained in the
specified collection.
-
trueif this collection changed as a result of the
call
- {VISDOC_LINK_0}
UnsupportedOperationException— if theretainAllmethod
is not supported by this Collection. - {VISDOC_LINK_1}
ClassCastException— if the types of one or more elements
in this collection are incompatible with the specified
collection (optional). - {VISDOC_LINK_2}
NullPointerException— if this collection contains one or more
null elements and the specified collection does not support null
elements (optional). - {VISDOC_LINK_3}
NullPointerException— if the specified collection is
null.
size
- the number of elements in this collection
toArray
the collection 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 returned array will be "safe" in that no references to it are
maintained by this collection. (In other words, this method must
allocate a new array even if this collection is backed by an array).
The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based
APIs.
- an array containing all of the elements in this collection