Sorted Set

TreeSet is an example of a sorted set, which is a set that maintains its elements in ascending order, sorted according to their natural ordering or according to a comparator that is supplied when the sorted set is created. Sorted sets are described by the SortedSet interface.

SortedSet, whose generic type is SortedSet<E>, extends Set. With two exceptions, the methods it inherits from Set behave identically on sorted sets as on other sets:

■ The Iterator instance returned from iterator() traverses the sorted set in order.

■ The array returned by toArray() contains the sorted set's elements in order.

NOTE: Although not guaranteed, the toString() methods of SortedSet implementations in the collections framework (such as TreeSet) return a string containing all of the sorted set's elements in order.

SortedSet's documentation requires that an implementation must provide the four standard constructors that I presented in my discussion of TreeSet. Furthermore, implementations of this interface must implement the methods that are described in Table 8-3.

Table 8-3. SortedSet-specific Methods

Method

Was this article helpful?

0 0

Post a comment