java::util::Collection Class Reference

Inheritance diagram for java::util::Collection:

Inheritance graph
java::util::AbstractCollectionjava::util::Listjava::util::Setjava::lang::Interfacejava::lang::Objectjava::util::AbstractListjava::util::AbstractSetjava::util::AbstractSequentialListjava::util::ArrayListjava::util::LinkedListjava::util::HashSetjava::util::TreeSetjava::util::SortedSet
[legend]
Collaboration diagram for java::util::Collection:

Collaboration graph
java::lang::Interfacejava::lang::Object
[legend]

List of all members.


Detailed Description

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. The SDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.

Bags or multisets (unordered collections that may contain duplicate elements) should implement this interface directly.

All general-purpose Collection implementation classes (which typically implement Collection indirectly through one of its subinterfaces) should provide two "standard" constructors: a void (no arguments) constructor, which creates an empty collection, and a constructor with a single argument of type Collection, which creates a new collection with the same elements as its argument. In effect, the latter constructor allows the user to copy any collection, producing an equivalent collection of the desired implementation type. There is no way to enforce this convention (as interfaces cannot contain constructors) but all of the general-purpose Collection implementations in the Java platform libraries comply.

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.

Author:
Josh Bloch
Version:
1.32, 02/23/00
See also:
Set

List

Map

SortedSet

SortedMap

HashSet

TreeSet

ArrayList

LinkedList

Vector

Collections

Arrays

AbstractCollection

Since:
1.2

Public Member Functions

virtual jint size () const =0
 Returns the number of elements in this collection.
virtual jboolean isEmpty () const =0
 Returns true if this collection contains no elements.
virtual jboolean contains (const ObjectRef &o) const =0
 Returns true if this collection contains the specified element.
virtual Ref< Iteratoriterator () const =0
 Returns an iterator over the elements in this collection.
virtual Array
< ObjectRef
toArray () const =0
 Returns an array containing all of the elements in this collection.
virtual jboolean add (const ObjectRef &o)=0
 Ensures that this collection contains the specified element (optional operation).
virtual jboolean remove (const ObjectRef &o)=0
 Removes a single instance of the specified element from this collection, if it is present (optional operation).
virtual jboolean containsAll (const Collection &c) const =0
 Returns true if this collection contains all of the elements in the specified collection.
virtual jboolean addAll (const Collection &c)=0
 Adds all of the elements in the specified collection to this collection (optional operation).
virtual jboolean removeAll (const Collection &c)=0
 Removes all this collection's elements that are also contained in the specified collection (optional operation).
virtual jboolean retainAll (const Collection &c)=0
 Retains only the elements in this collection that are contained in the specified collection (optional operation).
virtual void clear ()=0
 Removes all of the elements from this collection (optional operation).

Static Public Member Functions

static Ref< CollectionemptyCollection ()
 The empty collection (immutable).
static Ref< CollectionsingletonCollection (const ObjectRef &o)
 Returns an immutable collection containing only the specified object.
static Ref< CollectionsynchronizedCollection (const Ref< Collection > &c, const Ref< Sync > &sync=0)
 Returns a synchronized (thread-safe) collection backed by the specified collection.
static Ref< CollectionimmutableCollection (const Ref< Collection > &c)
 Returns an unmodifiable view of the specified collection.

Member Function Documentation

static Ref<Collection> java::util::Collection::emptyCollection (  )  [static]

The empty collection (immutable).

static Ref<Collection> java::util::Collection::singletonCollection ( const ObjectRef o  )  [static]

Returns an immutable collection containing only the specified object.

Parameters:
o the sole object to be stored in the returned set.
Returns:
an immutable collection containing only the specified object.

static Ref<Collection> java::util::Collection::synchronizedCollection ( const Ref< Collection > &  c,
const Ref< Sync > &  sync = 0 
) [static]

Returns a synchronized (thread-safe) collection backed by the specified collection.

In order to guarantee serial access, it is critical that all access to the backing collection is accomplished through the returned collection.

It is imperative that the user manually synchronize on the returned collection when iterating over it:

  Collection c = Collections.synchronizedCollection(myCollection);
     ...
  synchronized(c) {
      Iterator i = c.iterator(); // Must be in the synchronized block
      while (i.hasNext())
         foo(i.next());
  }
 
Failure to follow this advice may result in non-deterministic behavior.

The returned collection does not pass the hashCode and equals operations through to the backing collection, but relies on Object's equals and hashCode methods. This is necessary to preserve the contracts of these operations in the case that the backing collection is a set or a list.

The returned collection will be serializable if the specified collection is serializable.

Parameters:
c the collection to be "wrapped" in a synchronized collection.
Returns:
a synchronized view of the specified collection.

static Ref<Collection> java::util::Collection::immutableCollection ( const Ref< Collection > &  c  )  [static]

Returns an unmodifiable view of the specified collection.

This method allows modules to provide users with "read-only" access to internal collections. Query operations on the returned collection "read through" to the specified collection, and attempts to modify the returned collection, whether direct or via its iterator, result in an UnsupportedOperationException.

The returned collection does not pass the hashCode and equals operations through to the backing collection, but relies on Object's equals and hashCode methods. This is necessary to preserve the contracts of these operations in the case that the backing collection is a set or a list.

The returned collection will be serializable if the specified collection is serializable.

Parameters:
c the collection for which an unmodifiable view is to be returned.
Returns:
an unmodifiable view of the specified collection.

virtual jint java::util::Collection::size (  )  const [pure virtual]

Returns the number of elements in this collection.

If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:
the number of elements in this collection

Implemented in java::util::ArrayList, java::util::HashSet, java::util::LinkedList, and java::util::TreeSet.

virtual jboolean java::util::Collection::isEmpty (  )  const [pure virtual]

Returns true if this collection contains no elements.

Returns:
true if this collection contains no elements

Implemented in java::util::AbstractCollection, java::util::AbstractList, java::util::AbstractSet, java::util::ArrayList, java::util::HashSet, and java::util::TreeSet.

virtual jboolean java::util::Collection::contains ( const ObjectRef o  )  const [pure virtual]

Returns true if this collection contains the specified element.

More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).

Parameters:
o element whose presence in this collection is to be tested.
Returns:
true if this collection contains the specified element
Exceptions:
ClassCastException if the type of the specified element is incompatible with this collection (optional).
NullPointerException if the specified element is null and this collection does not support null elements (optional).

Implemented in java::util::AbstractCollection, java::util::AbstractList, java::util::AbstractSet, java::util::ArrayList, java::util::HashSet, java::util::LinkedList, and java::util::TreeSet.

virtual Ref<Iterator> java::util::Collection::iterator (  )  const [pure virtual]

Returns an iterator over the elements in this collection.

There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).

Returns:
an Iterator over the elements in this collection

Implemented in java::util::AbstractList, java::util::HashSet, and java::util::TreeSet.

virtual Array<ObjectRef> java::util::Collection::toArray (  )  const [pure virtual]

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

Implemented in java::util::AbstractCollection, java::util::AbstractList, java::util::AbstractSet, java::util::ArrayList, java::util::LinkedList, and java::util::TreeSet.

virtual jboolean java::util::Collection::add ( const ObjectRef o  )  [pure virtual]

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
Exceptions:
UnsupportedOperationException add is not supported by this collection.
ClassCastException class of the specified element prevents it from being added to this collection.
NullPointerException if the specified element is null and this collection does not support null elements.
IllegalArgumentException some aspect of this element prevents it from being added to this collection.

Implemented in java::util::AbstractCollection, java::util::AbstractList, java::util::AbstractSet, java::util::ArrayList, java::util::HashSet, java::util::LinkedList, java::util::List, and java::util::TreeSet.

virtual jboolean java::util::Collection::remove ( const ObjectRef o  )  [pure virtual]

Removes a single instance of the specified element from this collection, if it is present (optional operation).

More formally, removes an element e such that (o==null ? e==null : o.equals(e)), 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
Exceptions:
ClassCastException if the type of the specified element is incompatible with this collection (optional).
NullPointerException if the specified element is null and this collection does not support null elements (optional).
UnsupportedOperationException remove is not supported by this collection.

Implemented in java::util::AbstractCollection, java::util::AbstractList, java::util::AbstractSet, java::util::ArrayList, java::util::HashSet, java::util::LinkedList, java::util::List, and java::util::TreeSet.

virtual jboolean java::util::Collection::containsAll ( const Collection c  )  const [pure virtual]

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
Exceptions:
ClassCastException if the types of one or more elements in the specified collection are incompatible with this collection (optional).
NullPointerException if the specified collection contains one or more null elements and this collection does not support null elements (optional).
NullPointerException if the specified collection is null.
See also:
contains(Object)

Implemented in java::util::AbstractCollection, java::util::AbstractList, java::util::AbstractSet, and java::util::TreeSet.

virtual jboolean java::util::Collection::addAll ( const Collection c  )  [pure virtual]

Adds all of the elements in the specified collection to this collection (optional operation).

The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)

Parameters:
c elements to be inserted into this collection.
Returns:
true if this collection changed as a result of the call
Exceptions:
UnsupportedOperationException if this collection does not support the addAll method.
ClassCastException if the class of an element of the specified collection prevents it from being added to this collection.
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.
IllegalArgumentException some aspect of an element of the specified collection prevents it from being added to this collection.
See also:
add(Object)

Implemented in java::util::AbstractCollection, java::util::AbstractList, java::util::AbstractSet, java::util::ArrayList, java::util::LinkedList, java::util::List, and java::util::TreeSet.

virtual jboolean java::util::Collection::removeAll ( const Collection c  )  [pure virtual]

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
Exceptions:
UnsupportedOperationException if the removeAll method is not supported by this collection.
ClassCastException if the types of one or more elements in this collection are incompatible with the specified collection (optional).
NullPointerException if this collection contains one or more null elements and the specified collection does not support null elements (optional).
NullPointerException if the specified collection is null.
See also:
remove(Object)

contains(Object)

Implemented in java::util::AbstractCollection, java::util::AbstractList, java::util::AbstractSet, and java::util::TreeSet.

virtual jboolean java::util::Collection::retainAll ( const Collection c  )  [pure virtual]

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
Exceptions:
UnsupportedOperationException if the retainAll method is not supported by this Collection.
ClassCastException if the types of one or more elements in this collection are incompatible with the specified collection (optional).
NullPointerException if this collection contains one or more null elements and the specified collection does not support null elements (optional).
NullPointerException if the specified collection is null.
See also:
remove(Object)

contains(Object)

Implemented in java::util::AbstractCollection, java::util::AbstractList, java::util::AbstractSet, and java::util::TreeSet.

virtual void java::util::Collection::clear (  )  [pure virtual]

Removes all of the elements from this collection (optional operation).

This collection will be empty after this method returns unless it throws an exception.

Exceptions:
UnsupportedOperationException if the clear method is not supported by this collection.

Implemented in java::util::AbstractCollection, java::util::AbstractList, java::util::AbstractSet, java::util::ArrayList, java::util::HashSet, java::util::LinkedList, and java::util::TreeSet.


The documentation for this class was generated from the following file:
Generated on Fri May 16 11:56:47 2008 for CrossPlatformJavaLikeC++API by  doxygen 1.5.3