Java Reference
In-Depth Information
21.5.
Set
and
SortedSet
The
Set<E>
interface extends
Collection<E>
, providing a more specific con-
tract for its methods, but adds no new methods of its own. A collection
that is a
Set
contains no duplicate elements. If you add the same element
twice to a set (in other words, if you add two objects that are
equal
), the
first invocation will return
TRue
, while the second will return
false
. If after
this,
remove
is similarly invoked twice, the first
remove
of the element will
return
TRue
since the set was changed by removing the element, while
the second will return
false
since the element was no longer present. A
set may also contain at most one
null
element.
The
SortedSet<E>
interface extends
Set<E>
to specify an additional contrac-
titerators on such a set will always return the elements in a specified or-
der. By default this will be the element's natural order. In the implement-
ations of
SortedSet
provided in
java.util
you can also specify a
Comparator
object that will be used to order the elements instead of the natural or-
der.
SortedSet
adds some methods that make sense in an ordered set:
public Comparator<? super E>
comparator()
Returns the
Comparator
being used by this sorted set, or
null
if
the elements' natural order is being used. Note the use of the
lower bounded wildcard in the return type; any implementa-
tion of this interface should accept a comparator that is typed
the same way.
public E
first()
Returns the first (lowest) object in this set.
public E
last()
Returns the last (highest) object in this set.