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 sorted set 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 sorted set 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 ClassCastException for any elements e1 and e2 in the sorted set. Attempts to violate this restriction will cause the offending method or constructor invocation to throw a ClassCastException.

Note that the ordering maintained by a sorted set (whether or not an explicit comparator is provided) must be consistent with equals if the sorted set is to correctly implement the Set interface. (See the Comparable interface or Comparator interface 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 sorted set performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the sorted set, equal. The behavior of a sorted set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.

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

This interface is a member of the Java Collections Framework.

@author
Josh Bloch
@version
1.24, 06/28/04
@since
1.2
Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element, o, to this set if this set contains no element e such that (o==null ? e==null : o.equals(e)). If this set already contains the specified element, the call leaves this set unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements.

The stipulation above does not imply that sets must accept all elements; sets may refuse to add any particular element, including null, and throwing an exception, as described in the specification for Collection.add. Individual set implementations should clearly document any restrictions on the elements that they may contain.

Parameters
oelement to be added to this set.
Return
true if this set did not already contain the specified element.
Throws
UnsupportedOperationExceptionif the add method is not supported by this set.
ClassCastExceptionif the class of the specified element prevents it from being added to this set.
NullPointerExceptionif the specified element is null and this set does not support null elements.
IllegalArgumentExceptionif some aspect of the specified element prevents it from being added to this set.
Adds all of the elements in the specified collection to this set if they're not already present (optional operation). If the specified collection is also a set, the addAll operation effectively modifies this set so that its value is the union of the two sets. The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress.
Parameters
ccollection whose elements are to be added to this set.
Return
true if this set changed as a result of the call.
Throws
UnsupportedOperationExceptionif the addAll method is not supported by this set.
ClassCastExceptionif the class of some element of the specified collection prevents it from being added to this set.
NullPointerExceptionif the specified collection contains one or more null elements and this set does not support null elements, or if the specified collection is null.
IllegalArgumentExceptionif some aspect of some element of the specified collection prevents it from being added to this set.
See Also
Removes all of the elements from this set (optional operation). This set will be empty after this call returns (unless it throws an exception).
Throws
UnsupportedOperationExceptionif the clear method is not supported by this set.
Returns the comparator associated with this sorted set, or null if it uses its elements' natural ordering.
Return
the comparator associated with this sorted set, or null if it uses its elements' natural ordering.
Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).
Parameters
oelement whose presence in this set is to be tested.
Return
true if this set contains the specified element.
Throws
ClassCastExceptionif the type of the specified element is incompatible with this set (optional).
NullPointerExceptionif the specified element is null and this set does not support null elements (optional).
Returns true if this set contains all of the elements of the specified collection. If the specified collection is also a set, this method returns true if it is a subset of this set.
Parameters
ccollection to be checked for containment in this set.
Return
true if this set contains all of the elements of the specified collection.
Throws
ClassCastExceptionif the types of one or more elements in the specified collection are incompatible with this set (optional).
NullPointerExceptionif the specified collection contains one or more null elements and this set does not support null elements (optional).
NullPointerExceptionif the specified collection is null.
Compares the specified object with this set for equality. Returns true if the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set (or equivalently, every member of this set is contained in the specified set). This definition ensures that the equals method works properly across different implementations of the set interface.
Parameters
oObject to be compared for equality with this set.
Return
true if the specified Object is equal to this set.
Returns the first (lowest) element currently in this sorted set.
Return
the first (lowest) element currently in this sorted set.
Throws
NoSuchElementExceptionsorted set is empty.
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, where the hashcode of a null element is defined to be zero. 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 the Object.hashCode method.
Return
the hash code value for this set.
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+"\0");
Parameters
toElementhigh endpoint (exclusive) of the headSet.
Return
a view of the specified initial range of this sorted set.
Throws
ClassCastExceptionif 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.
NullPointerExceptionif toElement is null and this sorted set does not tolerate null elements.
IllegalArgumentExceptionif this set is itself a subSet, headSet, or tailSet, and toElement is not within the specified range of the subSet, headSet, or tailSet.
Returns true if this set contains no elements.
Return
true if this set contains no elements.
Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).
Return
an iterator over the elements in this set.
Returns the last (highest) element currently in this sorted set.
Return
the last (highest) element currently in this sorted set.
Throws
NoSuchElementExceptionsorted set is empty.
Removes the specified element from this set if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if the set contains such an element. Returns true if the set contained the specified element (or equivalently, if the set changed as a result of the call). (The set will not contain the specified element once the call returns.)
Parameters
oobject to be removed from this set, if present.
Return
true if the set contained the specified element.
Throws
ClassCastExceptionif the type of the specified element is incompatible with this set (optional).
NullPointerExceptionif the specified element is null and this set does not support null elements (optional).
UnsupportedOperationExceptionif the remove method is not supported by this set.
Removes from this set all of its elements that are contained in the specified collection (optional operation). If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.
Parameters
ccollection that defines which elements will be removed from this set.
Return
true if this set changed as a result of the call.
Throws
UnsupportedOperationExceptionif the removeAll method is not supported by this Collection.
ClassCastExceptionif the types of one or more elements in this set are incompatible with the specified collection (optional).
NullPointerExceptionif this set contains a null element and the specified collection does not support null elements (optional).
NullPointerExceptionif the specified collection is null.
Retains only the elements in this set that are contained in the specified collection (optional operation). In other words, removes from this set all of its elements that are not contained in the specified collection. If the specified collection is also a set, this operation effectively modifies this set so that its value is the intersection of the two sets.
Parameters
ccollection that defines which elements this set will retain.
Return
true if this collection changed as a result of the call.
Throws
UnsupportedOperationExceptionif the retainAll method is not supported by this Collection.
ClassCastExceptionif the types of one or more elements in this set are incompatible with the specified collection (optional).
NullPointerExceptionif this set contains a null element and the specified collection does not support null elements (optional).
NullPointerExceptionif the specified collection is null.
Returns the number of elements in this set (its cardinality). If this set contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
Return
the number of elements in this set (its cardinality).
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+"\0");
 
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+"\0", high);
 
Parameters
fromElementlow endpoint (inclusive) of the subSet.
toElementhigh endpoint (exclusive) of the subSet.
Return
a view of the specified range within this sorted set.
Throws
ClassCastExceptionif 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.
IllegalArgumentExceptionif 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.
NullPointerExceptionif fromElement or toElement is null and this sorted set does not tolerate null elements.
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+"\0");
Parameters
fromElementlow endpoint (inclusive) of the tailSet.
Return
a view of the specified final range of this sorted set.
Throws
ClassCastExceptionif 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.
NullPointerExceptionif fromElement is null and this sorted set does not tolerate null elements.
IllegalArgumentExceptionif this set is itself a subSet, headSet, or tailSet, and fromElement is not within the specified range of the subSet, headSet, or tailSet.
Returns an array containing all of the elements in this set. Obeys the general contract of the Collection.toArray method.
Return
an array containing all of the elements in this set.
Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. Obeys the general contract of the Collection.toArray(Object[]) method.
Parameters
athe array into which the elements of this set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Return
an array containing the elements of this set.
Throws
ArrayStoreExceptionthe runtime type of a is not a supertype of the runtime type of every element in this set.
NullPointerExceptionif the specified array is null.