Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.util

Interface SortedSet

java.lang.Object
|
+--java.util.Collection
   |
   +--java.util.Set
      |
      +--java.util.SortedSet

All Implemented Interfaces:

Set, Collection


public interface SortedSet

implements Set

A set which guarantees its iteration order. The elements in the set are related by the natural ordering if they are Comparable, or by the provided Comparator. Additional operations take advantage of the sorted nature of the set.

All elements entered in the set must be mutually comparable; in other words, k1.compareTo(k2) or comparator.compare(k1, k2) must not throw a ClassCastException. The ordering must be consistent with equals (see Comparator for this definition), if the map is to obey the general contract of the Set interface. If not, the results are well-defined, but probably not what you wanted.

It is recommended that all implementing classes provide four constructors: 1) one that takes no arguments and builds an empty set sorted by natural order of the elements; 2) one that takes a Comparator for the sorting order; 3) one that takes a Set and sorts according to the natural order of its elements; and 4) one that takes a SortedSet and sorts by the same comparator. Unfortunately, the Java language does not provide a way to enforce this.

Since:Authors:See Also:

Method Summary

java.util.Comparatorcomparator()

Returns the comparator used in sorting this set, or null if it is the elements' natural ordering.
java.lang.Objectfirst()

Returns the first (lowest sorted) element in the map.
java.util.SortedSetheadSet(java.lang.Object toElement)

Returns a view of the portion of the set strictly less than toElement.
java.lang.Objectlast()

Returns the last (highest sorted) element in the map.
java.util.SortedSetsubSet(java.lang.Object fromElement, java.lang.Object toElement)

Returns a view of the portion of the set greater than or equal to fromElement, and strictly less than toElement.
java.util.SortedSettailSet(java.lang.Object fromElement)

Returns a view of the portion of the set greater than or equal to fromElement.

Method Details

comparator

public Comparator comparator()

Returns the comparator used in sorting this set, or null if it is the elements' natural ordering.

Returns:


first

public Object first()

Returns the first (lowest sorted) element in the map.

Returns:


headSet

public SortedSet headSet(java.lang.Object toElement)

Returns a view of the portion of the set strictly less than toElement. The view is backed by this set, so changes in one show up in the other. The subset supports all optional operations of the original.

The returned set throws an IllegalArgumentException any time an element is used which is out of the range of toElement. Note that the endpoint is not included; if you want the endpoint, pass the successor object in to toElement. For example, for Strings, you can request headSet(limit + "\0").

Parameters:

Returns:

Throws:


last

public Object last()

Returns the last (highest sorted) element in the map.

Returns:


subSet

public SortedSet subSet(java.lang.Object fromElement, java.lang.Object toElement)

Returns a view of the portion of the set greater than or equal to fromElement, and strictly less than toElement. The view is backed by this set, so changes in one show up in the other. The subset supports all optional operations of the original.

The returned set throws an IllegalArgumentException any time an element is used which is out of the range of fromElement and toElement. Note that the lower endpoint is included, but the upper is not; if you want to change the inclusion or exclusion of an endpoint, pass the successor object in instead. For example, for Strings, you can request subSet(lowlimit + "\0", highlimit + "\0") to reverse the inclusiveness of both endpoints.

Parameters:

Returns:

Throws:


tailSet

public SortedSet tailSet(java.lang.Object fromElement)

Returns a view of the portion of the set greater than or equal to fromElement. The view is backed by this set, so changes in one show up in the other. The subset supports all optional operations of the original.

The returned set throws an IllegalArgumentException any time an element is used which is out of the range of fromElement. Note that the endpoint is included; if you do not want the endpoint, pass the successor object in to fromElement. For example, for Strings, you can request tailSet(limit + "\0").

Parameters:

Returns:

Throws: