java::util::TreeSet Class Reference

Inheritance diagram for java::util::TreeSet:

Inheritance graph
java::util::AbstractSetjava::util::AbstractCollectionjava::util::Collectionjava::util::Setjava::lang::Interfacejava::lang::Objectjava::util::SortedSet
[legend]
Collaboration diagram for java::util::TreeSet:

Collaboration graph
java::util::AbstractSetjava::util::AbstractCollectionjava::util::Collectionjava::util::Setjava::lang::Interfacejava::lang::Objectjava::util::SortedSet
[legend]

List of all members.


Detailed Description

This class implements the Set interface, backed by a TreeMap instance.

This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.

This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.

Note that this implementation is not synchronized. If multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be "wrapped" using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the set:

     SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
 

The Iterators returned by this class's iterator method are fail-fast: if the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

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

Set

HashSet

Comparable

Comparator

Collections::synchronizedSortedSet(SortedSet)

TreeMap

Since:
1.2

Public Member Functions

 TreeSet (const Ref< Comparator > &comp=0)
 Constructs a new, empty set, sorted according to the specified comparator.
 TreeSet (const Collection &c)
 Constructs a new set containing the elements in the specified collection, sorted according to the elements' natural order.
 TreeSet (const SortedSet &s)
 Constructs a new set containing the same elements as the specified sorted set, sorted according to the same ordering.
virtual jint size () const
 Returns the number of elements in this collection.
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 Ref< Iteratoriterator () const
 Returns an iterator over 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 set.
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 Ref< Comparatorcomparator () const
 Returns the comparator associated with this sorted set, or null if it uses its elements' natural ordering.
virtual Ref< SortedSetsubSet (const ObjectRef &fromElement, const ObjectRef &toElement) const
 Returns a view of the portion of this sorted set whose elements range from fromElement, inclusive, to toElement, exclusive.
virtual Ref< SortedSetheadSet (const ObjectRef &toElement) const
 Returns a view of the portion of this sorted set whose elements are strictly less than toElement.
virtual Ref< SortedSettailSet (const ObjectRef &fromElement) const
 Returns a view of the portion of this sorted set whose elements are greater than or equal to fromElement.
virtual ObjectRef first () const
 Returns the first (lowest) element currently in this sorted set.
virtual ObjectRef last () const
 Returns the last (highest) element currently in this sorted set.
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.

Constructor & Destructor Documentation

java::util::TreeSet::TreeSet ( const Ref< Comparator > &  comp = 0  ) 

Constructs a new, empty set, sorted according to the specified comparator.

All elements inserted into the set must be mutually comparable by the specified comparator: comparator.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the set. If the user attempts to add an element to the set that violates this constraint, the add(Object) call will throw a ClassCastException.

Parameters:
c the comparator that will be used to sort this set. A null value indicates that the elements' natural ordering should be used.

java::util::TreeSet::TreeSet ( const Collection c  ) 

Constructs a new set containing the elements in the specified collection, sorted according to the elements' natural order.

All keys inserted into the set must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a ClassCastException for any elements k1 and k2 in the set.

Parameters:
c The elements that will comprise the new set.
Exceptions:
ClassCastException if the keys in the specified collection are not comparable, or are not mutually comparable.
NullPointerException if the specified collection is null.

java::util::TreeSet::TreeSet ( const SortedSet s  ) 

Constructs a new set containing the same elements as the specified sorted set, sorted according to the same ordering.

Parameters:
s sorted set whose elements will comprise the new set.
Exceptions:
NullPointerException if the specified sorted set is null.


Member Function Documentation

virtual jint java::util::TreeSet::size (  )  const [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

Implements java::util::Collection.

virtual jboolean java::util::TreeSet::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::AbstractSet.

virtual jboolean java::util::TreeSet::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::AbstractSet.

virtual Array<ObjectRef> java::util::TreeSet::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::AbstractSet.

virtual Ref<Iterator> java::util::TreeSet::iterator (  )  const [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

Implements java::util::Collection.

virtual jboolean java::util::TreeSet::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::AbstractSet.

virtual jboolean java::util::TreeSet::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::AbstractSet.

virtual jboolean java::util::TreeSet::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::AbstractSet.

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

Adds all of the elements in the specified collection to this set.

Parameters:
c elements to be added
Returns:
true if this set changed as a result of the call.
Exceptions:
ClassCastException if the elements provided cannot be compared with the elements currently in the set.
NullPointerException of the specified collection is null.

Reimplemented from java::util::AbstractSet.

virtual jboolean java::util::TreeSet::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::AbstractSet.

virtual jboolean java::util::TreeSet::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::AbstractSet.

virtual void java::util::TreeSet::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::AbstractSet.

virtual Ref<Comparator> java::util::TreeSet::comparator (  )  const [virtual]

Returns the comparator associated with this sorted set, or null if it uses its elements' natural ordering.

Returns:
the comparator associated with this sorted set, or null if it uses its elements' natural ordering.

Implements java::util::SortedSet.

virtual Ref<SortedSet> java::util::TreeSet::subSet ( const ObjectRef fromElement,
const ObjectRef toElement 
) const [virtual]

Returns a view of the portion of this sorted set whose elements range from fromElement, inclusive, to toElement, exclusive.

(If fromElement and toElement are equal, the returned sorted set is empty.) The returned sorted set is backed by this sorted set, so changes in the returned sorted set are reflected in this sorted set, and vice-versa. The returned sorted set supports all optional set operations that this sorted set supports.

The sorted set returned by this method will throw an IllegalArgumentException if the user attempts to insert a element outside the specified range.

Note: this method always returns a half-open range (which includes its low endpoint but not its high endpoint). If you need a closed range (which includes both endpoints), and the element type allows for calculation of the successor a given value, merely request the subrange from lowEndpoint to successor(highEndpoint). For example, suppose that s is a sorted set of strings. The following idiom obtains a view containing all of the strings in s from low to high, inclusive:

 SortedSet sub = s.subSet(low, high+"");
 

A similar technique can be used to generate an open range (which contains neither endpoint). The following idiom obtains a view containing all of the Strings in s from low to high, exclusive:

 SortedSet sub = s.subSet(low+"", high);
 

Parameters:
fromElement low endpoint (inclusive) of the subSet.
toElement high endpoint (exclusive) of the subSet.
Returns:
a view of the specified range within this sorted set.
Exceptions:
ClassCastException if fromElement and toElement cannot be compared to one another using this set's comparator (or, if the set has no comparator, using natural ordering). Implementations may, but are not required to, throw this exception if fromElement or toElement cannot be compared to elements currently in the set.
IllegalArgumentException if fromElement is greater than toElement; or if this set is itself a subSet, headSet, or tailSet, and fromElement or toElement are not within the specified range of the subSet, headSet, or tailSet.
NullPointerException if fromElement or toElement is null and this sorted set does not tolerate null elements.

Implements java::util::SortedSet.

virtual Ref<SortedSet> java::util::TreeSet::headSet ( const ObjectRef toElement  )  const [virtual]

Returns a view of the portion of this sorted set whose elements are strictly less than toElement.

The returned sorted set is backed by this sorted set, so changes in the returned sorted set are reflected in this sorted set, and vice-versa. The returned sorted set supports all optional set operations.

The sorted set returned by this method will throw an IllegalArgumentException if the user attempts to insert a element outside the specified range.

Note: this method always returns a view that does not contain its (high) endpoint. If you need a view that does contain this endpoint, and the element type allows for calculation of the successor a given value, merely request a headSet bounded by successor(highEndpoint). For example, suppose that s is a sorted set of strings. The following idiom obtains a view containing all of the strings in s that are less than or equal to high:

    SortedSet head = s.headSet(high+"");

Parameters:
toElement high endpoint (exclusive) of the headSet.
Returns:
a view of the specified initial range of this sorted set.
Exceptions:
ClassCastException if toElement is not compatible with this set's comparator (or, if the set has no comparator, if toElement does not implement Comparable). Implementations may, but are not required to, throw this exception if toElement cannot be compared to elements currently in the set.
NullPointerException if toElement is null and this sorted set does not tolerate null elements.
IllegalArgumentException if this set is itself a subSet, headSet, or tailSet, and toElement is not within the specified range of the subSet, headSet, or tailSet.

Implements java::util::SortedSet.

virtual Ref<SortedSet> java::util::TreeSet::tailSet ( const ObjectRef fromElement  )  const [virtual]

Returns a view of the portion of this sorted set whose elements are greater than or equal to fromElement.

The returned sorted set is backed by this sorted set, so changes in the returned sorted set are reflected in this sorted set, and vice-versa. The returned sorted set supports all optional set operations.

The sorted set returned by this method will throw an IllegalArgumentException if the user attempts to insert a element outside the specified range.

Note: this method always returns a view that contains its (low) endpoint. If you need a view that does not contain this endpoint, and the element type allows for calculation of the successor a given value, merely request a tailSet bounded by successor(lowEndpoint). For example, suppose that s is a sorted set of strings. The following idiom obtains a view containing all of the strings in s that are strictly greater than low:

    SortedSet tail = s.tailSet(low+"");

Parameters:
fromElement low endpoint (inclusive) of the tailSet.
Returns:
a view of the specified final range of this sorted set.
Exceptions:
ClassCastException if fromElement is not compatible with this set's comparator (or, if the set has no comparator, if fromElement does not implement Comparable). Implementations may, but are not required to, throw this exception if fromElement cannot be compared to elements currently in the set.
NullPointerException if fromElement is null and this sorted set does not tolerate null elements.
IllegalArgumentException if this set is itself a subSet, headSet, or tailSet, and fromElement is not within the specified range of the subSet, headSet, or tailSet.

Implements java::util::SortedSet.

virtual ObjectRef java::util::TreeSet::first (  )  const [virtual]

Returns the first (lowest) element currently in this sorted set.

Returns:
the first (lowest) element currently in this sorted set.
Exceptions:
NoSuchElementException sorted set is empty.

Implements java::util::SortedSet.

virtual ObjectRef java::util::TreeSet::last (  )  const [virtual]

Returns the last (highest) element currently in this sorted set.

Returns:
the last (highest) element currently in this sorted set.
Exceptions:
NoSuchElementException sorted set is empty.

Implements java::util::SortedSet.

virtual jboolean java::util::TreeSet::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::util::AbstractSet.

virtual jint java::util::TreeSet::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::util::AbstractSet.

virtual String java::util::TreeSet::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::AbstractSet.


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