Overview | Package | Class | Tree | Index | Help | |||
PREV CLASS | NEXT CLASS | FRAMES | NO FRAMES | ||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
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 only JDK class whose natural ordering does not satisfy this constraint is BigDecimal.
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.
Method Summary | |
Comparator | comparator()
Returns the Comparator associated with this SortedSet, or null if it uses its elements' natural ordering. |
java.lang.Object | first()
Returns the first (lowest) element currently in this SortedSet. |
SortedSet | headSet(java.lang.Object toElement)
Returns a view of the portion of this SortedSet whose elements are strictly less than toElement. |
java.lang.Object | last()
Returns the last (highest) element currently in this SortedSet. |
SortedSet | subSet(java.lang.Object fromElement,
java.lang.Object toElement)
Returns a view of the portion of this SortedSet whose elements range from fromElement, inclusive, to toElement, exclusive. |
SortedSet | tailSet(java.lang.Object fromElement)
Returns a view of the portion of this SortedSet whose elements are greater than or equal to fromElement. |
Method Detail |
public Comparator comparator()
public SortedSet subSet(java.lang.Object fromElement, java.lang.Object toElement)
fromElement
- low endpoint (inclusive) of the subSet.
toElement
- high endpoint (exclusive) of the subSet.public SortedSet headSet(java.lang.Object toElement)
The Set returned by this method will throw an IllegalArgumentException if the user attempts to insert a element outside the specified range.
toElement
- high endpoint (exclusive) of the headSet.public SortedSet tailSet(java.lang.Object fromElement)
toElement
- high endpoint (exclusive) of the tailSet.public java.lang.Object first()
public java.lang.Object last()
Overview | Package | Class | Tree | Index | Help | |||
PREV CLASS | NEXT CLASS | FRAMES | NO FRAMES | ||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |