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
The root interface in the collection hierarchy. A collection
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, typically

NullPointerException 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


Instance methods
  • 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 inherited from Iterable

Instance methods

add

public function add (
o:Object) : Boolean

Ensures that this collection contains the specified element (optional
operation). Returns true if this collection changed as a
result of the call. (Returns false if this collection does
not 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.

Parameters:
o:
element whose presence in this collection is to be ensured.
Returns:
  • true if this collection changed as a result of the
    call
Throws:
  • {VISDOC_LINK_0}UnsupportedOperationExceptionadd is 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

public function addAll (
c:Collection) : Boolean

Adds all of the elements in the specified collection to this collection
(optional operation).
Parameters:
c:
elements to be inserted into this collection.
Returns:
  • true if this collection changed as a result of the
    call
Throws:
  • {VISDOC_LINK_0}UnsupportedOperationException — if this collection does not
    support the addAll method.
  • {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 is null.
  • {VISDOC_LINK_3}IllegalArgumentException — some aspect of an element of the
    specified collection prevents it from being added to this collection.
See also:

clear

public function clear (
) : void

Removes all of the elements from this collection (optional operation).
This collection will be empty after this method returns unless it
throws an exception.
Throws:
  • {VISDOC_LINK_0}UnsupportedOperationException — if the clear method is
    not supported by this collection.

contains

public function contains (
o:Object) : Boolean

Returns true if this collection contains the specified
element.
Parameters:
o:
element whose presence in this collection is to be tested.
Returns:
  • true if this collection contains the specified
    element
Throws:
  • {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

public function containsAll (
c:Collection) : Boolean

Returns true if this collection contains all of the elements
in the specified collection.
Parameters:
c:
collection to be checked for containment in this collection.
Returns:
  • true if this collection contains all of the elements
    in the specified collection
Throws:
  • {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.
See also:

isEmpty

public function isEmpty (
) : Boolean

Returns true if this collection contains no elements.
Returns:
  • true if this collection contains no elements

remove

public function remove (
o:Object) : Boolean

Removes a single instance of the specified element from this
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).
Parameters:
o:
element to be removed from this collection, if present.
Returns:
  • true if this collection changed as a result of the
    call
Throws:
  • {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}UnsupportedOperationExceptionremove is not supported
    by this collection.

removeAll

public function removeAll (
c:Collection) : Boolean

Removes all this collection's elements that are also contained in the
specified collection (optional operation). After this call returns,
this collection will contain no elements in common with the specified
collection.
Parameters:
c:
elements to be removed from this collection.
Returns:
  • true if this collection changed as a result of the
    call
Throws:
  • {VISDOC_LINK_0}UnsupportedOperationException — if the removeAll method
    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.
See also:

retainAll

public function retainAll (
c:Collection) : Boolean

Retains only the elements in this collection that are contained in the
specified collection (optional operation). In other words, removes from
this collection all of its elements that are not contained in the
specified collection.
Parameters:
c:
elements to be retained in this collection.
Returns:
  • true if this collection changed as a result of the
    call
Throws:
  • {VISDOC_LINK_0}UnsupportedOperationException — if the retainAll method
    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.
See also:

size

public function size (
) : uint

Returns the number of elements in this collection.
Returns:
  • the number of elements in this collection

toArray

public function toArray (
) : Array

Returns an array containing all of the elements in this collection. If
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.

Returns:
  • an array containing all of the elements in this collection