java::util::AbstractSet Class Reference

Inheritance diagram for java::util::AbstractSet:

Inheritance graph
java::util::HashSetjava::util::TreeSetjava::util::AbstractCollectionjava::util::Collectionjava::util::Setjava::lang::Interfacejava::lang::Object
[legend]
Collaboration diagram for java::util::AbstractSet:

Collaboration graph
java::util::AbstractCollectionjava::util::Collectionjava::util::Setjava::lang::Interfacejava::lang::Object
[legend]

List of all members.


Detailed Description

This class provides a skeletal implementation of the Set interface to minimize the effort required to implement this interface.

The process of implementing a set by extending this class is identical to that of implementing a Collection by extending AbstractCollection, except that all of the methods and constructors in subclasses of this class must obey the additional constraints imposed by the Set interface (for instance, the add method must not permit addition of multiple intances of an object to a set).

Note that this class does not override any of the implementations from the AbstractCollection class. It merely adds implementations for equals and hashCode.

Author:
Josh Bloch
Version:
1.17, 12/03/01
See also:
Collection

AbstractCollection

Set

Since:
1.2

Public Member Functions

virtual jboolean isEmpty () const
 Returns true if this collection contains no elements.
virtual jboolean contains (const ObjectRef &o) const
 Returns true if this collection contains the specified element.
virtual Array
< ObjectRef
toArray () const
 Returns an array containing all of the elements in this collection.
virtual jboolean add (const ObjectRef &o)
 Ensures that this collection contains the specified element (optional operation).
virtual jboolean remove (const ObjectRef &o)
 Removes a single instance of the specified element from this collection, if it is present (optional operation).
virtual jboolean containsAll (const Collection &c) const
 Returns true if this collection contains all of the elements in the specified collection.
virtual jboolean addAll (const Collection &c)
 Adds all of the elements in the specified collection to this collection (optional operation).
virtual jboolean removeAll (const Collection &c)
 Removes from this collection all of its elements that are contained in the specified collection (optional operation).
virtual jboolean retainAll (const Collection &c)
 Retains only the elements in this collection that are contained in the specified collection (optional operation).
virtual void clear ()
 Removes all of the elements from this collection (optional operation).
virtual jboolean equals (const Object &o) const
 Compares the specified object with this set for equality.
virtual jint hashCode () const
 Returns the hash code value for this set.
virtual String toString () const
 Returns a string representation of this set.

Protected Member Functions

 AbstractSet ()
 Sole constructor.

Constructor & Destructor Documentation

java::util::AbstractSet::AbstractSet (  )  [protected]

Sole constructor.

(For invocation by subclass constructors, typically implicit.)


Member Function Documentation

virtual jboolean java::util::AbstractSet::isEmpty (  )  const [virtual]

Returns true if this collection contains no elements.

This implementation returns size() == 0.

Returns:
true if this collection contains no elements.

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::HashSet, and java::util::TreeSet.

virtual jboolean java::util::AbstractSet::contains ( const ObjectRef o  )  const [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)).

This implementation iterates over the elements in the collection, checking each element in turn for equality with the specified element.

Parameters:
o object to be checked for containment in this collection.
Returns:
true if this collection contains the specified element.

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::HashSet, and java::util::TreeSet.

virtual Array<ObjectRef> java::util::AbstractSet::toArray (  )  const [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 the collection. (In other words, this method must allocate a new array even if the collection is backed by an Array). The caller is thus free to modify the returned array.

This implementation allocates the array to be returned, and iterates over the elements in the collection, storing each object reference in the next consecutive element of the array, starting with element 0.

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

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::TreeSet.

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

Ensures that this collection contains the specified element (optional operation).

Returns true if the 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 the 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.

This implementation always throws an UnsupportedOperationException.

Parameters:
o element whose presence in this collection is to be ensured.
Returns:
true if the collection changed as a result of the call.
Exceptions:
UnsupportedOperationException if the add method is not supported by this collection.
NullPointerException if this collection does not permit null elements, and the specified element is null.
ClassCastException if the class of the specified element prevents it from being added to this collection.
IllegalArgumentException if some aspect of this element prevents it from being added to this collection.

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::HashSet, and java::util::TreeSet.

virtual jboolean java::util::AbstractSet::remove ( const ObjectRef o  )  [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 the collection contains one or more such elements. Returns true if the collection contained the specified element (or equivalently, if the collection changed as a result of the call).

This implementation iterates over the collection looking for the specified element. If it finds the element, it removes the element from the collection using the iterator's remove method.

Note that this implementation throws an UnsupportedOperationException if the iterator returned by this collection's iterator method does not implement the remove method and this collection contains the specified object.

Parameters:
o element to be removed from this collection, if present.
Returns:
true if the collection contained the specified element.
Exceptions:
UnsupportedOperationException if the remove method is not supported by this collection.

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::HashSet, and java::util::TreeSet.

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

Returns true if this collection contains all of the elements in the specified collection.

This implementation iterates over the specified collection, checking each element returned by the iterator in turn to see if it's contained in this collection. If all elements are so contained true is returned, otherwise false.

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:
NullPointerException if the specified collection is null.
See also:
contains(Object)

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::TreeSet.

virtual jboolean java::util::AbstractSet::addAll ( const Collection c  )  [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.)

This implementation iterates over the specified collection, and adds each object returned by the iterator to this collection, in turn.

Note that this implementation will throw an UnsupportedOperationException unless add is overridden (assuming the specified collection is non-empty).

Parameters:
c collection whose elements are to be added to 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.
NullPointerException if the specified collection is null.
See also:
add(Object)

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::TreeSet.

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

Removes from this collection all of its elements that are contained in the specified collection (optional operation).

This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection. If it's so contained, it's removed from this collection with the iterator's remove method.

Note that this implementation will throw an UnsupportedOperationException if the iterator returned by the iterator method does not implement the remove method and this collection contains one or more 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.
NullPointerException if the specified collection is null.
See also:
remove(Object)

contains(Object)

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::TreeSet.

virtual jboolean java::util::AbstractSet::retainAll ( const Collection c  )  [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.

This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection. If it's not so contained, it's removed from this collection with the iterator's remove method.

Note that this implementation will throw an UnsupportedOperationException if the iterator returned by the iterator method does not implement the remove method and this collection contains one or more elements not present 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.
NullPointerException if the specified collection is null.
See also:
remove(Object)

contains(Object)

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::TreeSet.

virtual void java::util::AbstractSet::clear (  )  [virtual]

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

The collection will be empty after this call returns (unless it throws an exception).

This implementation iterates over this collection, removing each element using the Iterator.remove operation. Most implementations will probably choose to override this method for efficiency.

Note that this implementation will throw an UnsupportedOperationException if the iterator returned by this collection's iterator method does not implement the remove method and this collection is non-empty.

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

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::HashSet, and java::util::TreeSet.

virtual jboolean java::util::AbstractSet::equals ( const Object o  )  const [virtual]

Compares the specified object with this set for equality.

Returns true if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set. This ensures that the equals method works properly across different implementations of the Set interface.

This implementation first checks if the specified object is this set; if so it returns true. Then, it checks if the specified object is a set whose size is identical to the size of this set; if not, it it returns false. If so, it returns containsAll((Collection) o).

Parameters:
o Object to be compared for equality with this set.
Returns:
true if the specified object is equal to this set.

Reimplemented from java::lang::Object.

Reimplemented in java::util::TreeSet.

virtual jint java::util::AbstractSet::hashCode (  )  const [virtual]

Returns the hash code value for this set.

The hash code of a set is defined to be the sum of the hash codes of the elements in the set. This ensures that s1.equals(s2) implies that s1.hashCode()==s2.hashCode() for any two sets s1 and s2, as required by the general contract of Object.hashCode.

This implementation enumerates over the set, calling the hashCode method on each element in the collection, and adding up the results.

Returns:
the hash code value for this set.

Reimplemented from java::lang::Object.

Reimplemented in java::util::TreeSet.

virtual String java::util::AbstractSet::toString (  )  const [virtual]

Returns a string representation of this set.

The string representation consists of a list of the set's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

This implementation creates an empty string buffer, appends a left square bracket, and iterates over the collection appending the string representation of each element in turn. After appending each element except the last, the string ", " is appended. Finally a right bracket is appended. A string is obtained from the string buffer, and returned.

Returns:
a string representation of this set.

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::TreeSet.


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