Java Platform 1.2
Beta 4

Interface java.util.SortedSet

All Known Implementing Classes:
TreeSet

public abstract interface SortedSet
extends Set
A Set that further guarantees that its iterator will traverse the Set in ascending element order, sorted according to the natural ordering of its elements (see Comparable), or by a Comparator provided at SortedSet creation time. Several additional operations are provided to take advantage of the ordering. (This interface is the Set analogue of SortedMap.)

All elements inserted into an SortedSet must implement the Comparable interface (or be accepted by the specified Comparator). Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) (or comparator.compare(e1, e2)) must not throw a typeMismatchException for any elements e1 and e2 in the SortedSet. Attempts to violate this restriction will cause the offending method or constructor invocation to throw a ClassCastException.

Note that the ordering maintained by a SortedSet (whether or not an explicit Comparator is provided) must be total if the SortedSet is to correctly implement the Set interface. (See Comparable or Comparator for a definition of total ordering.) This is so because the Set interface is defined in terms of the equals operation, but a SortedSet 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 SortedSet, equal. The behavior of a SortedSet is well-defined even if its ordering is strictly partial; it just fails to obey the general contract of the Set interface.

All general-purpose SortedSet implementation classes should provide four "standard" constructors: 1) A void (no arguments) constructor, which creates an empty SortedSet sorted according to the natural order of its elements. 2) A constructor with a single argument of type Comparator, which creates an empty SortedSet sorted according to the specified Comparator. 3) A constructor with a single argument of type Collection, which creates a new Set with the same elements as its argument, sorted according to the elements' natural ordering. 4) A constructor with a single argument of type SortedSet, which creates a new SortedSet with the same elements and the same ordering as the input SortedSet. There is no way to enforce this recommendation (as interfaces cannot contain constructors) but the JDK implementation (TreeSet) complies.

Since:
JDK1.2
See Also:
Set, TreeSet, SortedMap, Collection, Comparable, Comparator, ClassCastException

Method Summary
 Comparator comparator()
          Returns the Comparator associated with this SortedSet, or null if it uses its elements' natural ordering.
 Object first()
          Returns the first (lowest) element currently in this SortedSet.
 SortedSet headSet(Object toElement)
          Returns a view of the portion of this SortedSet whose elements are strictly less than toElement.
 Object last()
          Returns the last (highest) element currently in this SortedSet.
 SortedSet subSet(Object fromElement, Object toElement)
          Returns a view of the portion of this SortedSet whose elements range from fromElement, inclusive, to toElement, exclusive.
 SortedSet tailSet(Object fromElement)
          Returns a view of the portion of this SortedSet whose elements are greater than or equal to fromElement.
 
Methods inherited from interface java.util.Set
add , addAll , clear , contains , containsAll , equals , hashCode , isEmpty , iterator , remove , removeAll , retainAll , size , toArray , toArray
 

Method Detail

comparator

public Comparator comparator()
Returns the Comparator associated with this SortedSet, or null if it uses its elements' natural ordering.
Returns:
the Comparator associated with this SortedSet, or null if it uses its elements' natural ordering.

subSet

public SortedSet subSet(Object fromElement,
                        Object toElement)
Returns a view of the portion of this SortedSet whose elements range from fromElement, inclusive, to toElement, exclusive. The returned SortedSet is backed by this SortedSet, so changes in the returned SortedSet are reflected in this SortedSet, and vice-versa. The returned Set supports all optional Set operations.

The 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 Set of Strings. The following idiom obtains a view containing all of the Strings in s from low to high, inclusive:

    Set sub = s.subSet(low, high+"\0");
A similarly 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:
    Set sub = s.subSet(low+"\0", 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 SortedSet.
Throws:
ClassCastException - fromElement or toElement cannot be compared with the elements currently in the SortedSet. (Implementations may, but are not required to, throw this exception under these circumstances.)
NullPointerException - fromElement or toElement is null and this SortedSet does not tolerate null elements.
IllegalArgumentException - fromElement is greater than toElement.

headSet

public SortedSet headSet(Object toElement)
Returns a view of the portion of this SortedSet whose elements are strictly less than toElement. The returned SortedSet is backed by this SortedSet, so changes in the returned SortedSet are reflected in this SortedSet, and vice-versa. The returned Set supports all optional Set operations.

The 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 Set of Strings. The following idiom obtains a view containing all of the Strings in s that are less than or equal to high:

    Set head = s.headSet(high+"\0");
Parameters:
toElement - high endpoint (exclusive) of the headSet.
Returns:
a view of the specified initial range of this SortedSet.
Throws:
ClassCastException - toElement cannot be compared with the elements currently in the SortedSet. (Implementations may, but are not required to, throw this exception under these circumstances.)
NullPointerException - toElement is null and this SortedSet does not tolerate null elements.

tailSet

public SortedSet tailSet(Object fromElement)
Returns a view of the portion of this SortedSet whose elements are greater than or equal to fromElement. The returned SortedSet is backed by this SortedSet, so changes in the returned SortedSet are reflected in this SortedSet, and vice-versa. The returned Set supports all optional Set operations.

The 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 Set of Strings. The following idiom obtains a view containing all of the Strings in s that are strictly greater than low:

    Set tail = s.tailSet(low+"\0");
Parameters:
fromElement - low endpoint (inclusive) of the tailSet.
Returns:
a view of the specified final range of this SortedSet.
Throws:
ClassCastException - fromElement cannot be compared with the elements currently in the SortedSet. (Implementations may, but are not required to, throw this exception under these circumstances.)
NullPointerException - fromElement is null and this SortedSet does not tolerate null elements.

first

public Object first()
Returns the first (lowest) element currently in this SortedSet.
Returns:
the first (lowest) element currently in this SortedSet.
Throws:
NoSuchElementException - Set is empty.

last

public Object last()
Returns the last (highest) element currently in this SortedSet.
Returns:
the last (highest) element currently in this SortedSet.
Throws:
NoSuchElementException - Set is empty.

Java Platform 1.2
Beta 4

Submit a bug or feature
Submit comments/suggestions about new javadoc look
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.
This documentation was generated with a post-Beta4 version of Javadoc.