|
|
Java Collection Framework - SortedSet Interface |
The Collection Framework provides
a special Set interface for maintaining
elements in a sorted order called
SortedSet.
Comparator
|
comparator()
Returns the comparator associated
with this sorted set, or null if it uses its elements' natural ordering.
|
Object
|
first( )
Returns the first (lowest) element
currently in this sorted set.
|
SortedSet
|
headSet ( Object toElement)
Returns a view of the portion
of this sorted set whose elements
are strictly less than toElement.
|
Object
|
last( )
Returns the last (highest) element
currently in this sorted set.
|
SortedSet
|
subSet ( Object fromElement,
Object toElement)
Returns a view of the portion
of this sorted set whose elements
range from fromElement,
inclusive, to toElement,
exclusive.
|
SortedSet
|
tailSet ( Object fromElement)
Returns a view of the portion
of this sorted set whose elements
are greater than or equal to fromElement.
|
The interface provides access methods
to the ends of the set as well to
subsets of the set. When working with
subsets of the list, changes to the
subset are reflected in the source
set. In addition changes to the source
set are reflected in the subset. This
works because subsets are identical
by elements at the end point, not
indices. In addition , if the formElement
is part of the source set , it is
part of the subset. However, if the
toElement is part of the source ser,
it is not part of the subset. If you
would like a particular to-element
to be in the subset, you must find
the next element. In the case of a
string, the next element is the same
strong with a null character appended
(string+”\0”).;
The element added to a SortedSet must
either implement Comparable or you
must provide a Comparator to the constructor
to its implementation class: TreeSet.
This example uses the reverse order
Comprator available from the Collection
calss.
Comparator comparator= Collections.reverseOrder();
Set reverseSer= new TreeSet(comparator);
revereseSet.add("one");
revereseSet.add("two");
revereseSet.add("three");
revereseSet.add("four");
revereseSet.add("one");
System.out.println(reverseSet);
Output of this program
[two, three, one, four]
Explore
the other Interface and Classes of
Java Collection Framework
Collection
Interface
Iterator
Interface
Set
Interface
List
Interface
ListIterator
Interface
Map
Interface
SortedSet
Interface
SortedMap
Interface
HashSet
& TreeSet Classes
ArrayList
& LinkedList Classes
HashMap
& Treemap Classes
Vector
and Stack Classes
|
|