public class TreeSet extends AbstractSet implements SortedSet, java.lang.Cloneable, java.io.Serializable, Rollbackable
Comparator
.Most operations are O(log n), but there is so much overhead that this makes small sets expensive. Note that the ordering must be consistent with equals to correctly implement the Set interface. If this condition is violated, the set is still well-behaved, but you may have suprising results when comparing it to other sets.
This implementation is not synchronized. If you need to share this between
multiple threads, do something like:
SortedSet s
= Collections.synchronizedSortedSet(new TreeSet(...));
The iterators are fail-fast, meaning that any structural
modification, except for remove()
called on the iterator
itself, cause the iterator to throw a
ConcurrentModificationException
rather than exhibit
non-deterministic behavior.
Collection
,
Set
,
HashSet
,
LinkedHashSet
,
Comparable
,
Comparator
,
Collections.synchronizedSortedSet(SortedSet)
,
TreeMap
,
Serialized Form$CHECKPOINT, $RECORD$$CHECKPOINT
Constructor and Description |
---|
TreeSet()
Construct a new TreeSet whose backing TreeMap using the "natural"
ordering of keys.
|
TreeSet(Collection collection)
Construct a new TreeSet whose backing TreeMap uses the "natural"
orering of the keys and which contains all of the elements in the
supplied Collection.
|
TreeSet(java.util.Comparator comparator)
Construct a new TreeSet whose backing TreeMap uses the supplied
Comparator.
|
TreeSet(SortedSet sortedSet)
Construct a new TreeSet, using the same key ordering as the supplied
SortedSet and containing all of the elements in the supplied SortedSet.
|
Modifier and Type | Method and Description |
---|---|
void |
$COMMIT(long timestamp)
Commit changes up to the given timestamp, but not including changes
made at timestamp and afterward.
|
void |
$RESTORE(long timestamp,
boolean trim)
Restore a previous state to all the private fields of this rollbackable
object.
|
boolean |
add(java.lang.Object obj)
Adds the spplied Object to the Set if it is not already in the Set;
returns true if the element is added, false otherwise.
|
boolean |
addAll(Collection c)
Adds all of the elements in the supplied Collection to this TreeSet.
|
void |
clear()
Removes all elements in this Set.
|
java.lang.Object |
clone()
Returns a shallow copy of this Set.
|
java.util.Comparator |
comparator()
Returns this Set's comparator.
|
boolean |
contains(java.lang.Object obj)
Returns true if this Set contains the supplied Object, false otherwise.
|
java.lang.Object |
first()
Returns the first (by order) element in this Set.
|
SortedSet |
headSet(java.lang.Object to)
Returns a view of this Set including all elements less than
to . |
boolean |
isEmpty()
Returns true if this Set has size 0, false otherwise.
|
java.util.Iterator |
iterator()
Returns in Iterator over the elements in this TreeSet, which traverses
in ascending order.
|
java.lang.Object |
last()
Returns the last (by order) element in this Set.
|
boolean |
remove(java.lang.Object obj)
If the supplied Object is in this Set, it is removed, and true is
returned; otherwise, false is returned.
|
int |
size()
Returns the number of elements in this Set
|
SortedSet |
subSet(java.lang.Object from,
java.lang.Object to)
Returns a view of this Set including all elements greater or equal to
from and less than to (a half-open interval). |
SortedSet |
tailSet(java.lang.Object from)
Returns a view of this Set including all elements greater or equal to
from . |
equals, hashCode, removeAll
$GET$CHECKPOINT, $SET$CHECKPOINT, containsAll, retainAll, toArray, toArray, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
$GET$CHECKPOINT, $SET$CHECKPOINT
public TreeSet()
Comparable
public TreeSet(java.util.Comparator comparator)
comparator
- the Comparator this Set will usepublic TreeSet(Collection collection)
collection
- the new Set will be initialized with all
of the elements in this Collectionjava.lang.ClassCastException
- if the elements of the collection are not
comparablejava.lang.NullPointerException
- if the collection is nullComparable
public TreeSet(SortedSet sortedSet)
sortedSet
- the new TreeSet will use this SortedSet's comparator
and will initialize itself with all its elementsjava.lang.NullPointerException
- if sortedSet is nullpublic boolean add(java.lang.Object obj)
add
in interface Collection
add
in interface Set
add
in class AbstractCollection
obj
- the Object to be added to this Setjava.lang.ClassCastException
- if the element cannot be compared with objects
already in the setpublic boolean addAll(Collection c)
addAll
in interface Collection
addAll
in interface Set
addAll
in class AbstractCollection
c
- The collection to addjava.lang.NullPointerException
- if c is nulljava.lang.ClassCastException
- if an element in c cannot be compared with
objects already in the setAbstractCollection.add(Object)
public void clear()
clear
in interface Collection
clear
in interface Set
clear
in class AbstractCollection
Iterator.remove()
public java.lang.Object clone()
clone
in class java.lang.Object
public java.util.Comparator comparator()
comparator
in interface SortedSet
public boolean contains(java.lang.Object obj)
contains
in interface Collection
contains
in interface Set
contains
in class AbstractCollection
obj
- the Object to check forjava.lang.ClassCastException
- if obj cannot be compared with objects
already in the setpublic java.lang.Object first()
public SortedSet headSet(java.lang.Object to)
to
. The returned set is backed by the original, so changes
in one appear in the other. The subset will throw an{headSet
in interface SortedSet
to
- the (exclusive) cutoff pointjava.lang.ClassCastException
- if to
is not compatible with
the comparator (or is not Comparable, for natural ordering)java.lang.NullPointerException
- if to is null, but the comparator does not
tolerate null elementspublic boolean isEmpty()
isEmpty
in interface Collection
isEmpty
in interface Set
isEmpty
in class AbstractCollection
AbstractCollection.size()
public java.util.Iterator iterator()
iterator
in interface Collection
iterator
in interface Set
iterator
in class AbstractCollection
public java.lang.Object last()
public boolean remove(java.lang.Object obj)
remove
in interface Collection
remove
in interface Set
remove
in class AbstractCollection
obj
- the Object to remove from this Setjava.lang.ClassCastException
- if obj cannot be compared to set elementsIterator.remove()
public int size()
size
in interface Collection
size
in interface Set
size
in class AbstractCollection
public SortedSet subSet(java.lang.Object from, java.lang.Object to)
from
and less than to
(a half-open interval).
The returned set is backed by the original, so changes in one appear in
the other. The subset will throw an {subSet
in interface SortedSet
from
- the (inclusive) low cutoff pointto
- the (exclusive) high cutoff pointjava.lang.ClassCastException
- if either cutoff is not compatible with
the comparator (or is not Comparable, for natural ordering)java.lang.NullPointerException
- if from or to is null, but the comparator
does not tolerate null elementsjava.lang.IllegalArgumentException
- if from is greater than topublic SortedSet tailSet(java.lang.Object from)
from
. The returned set is backed by the original, so
changes in one appear in the other. The subset will throw an{tailSet
in interface SortedSet
from
- the (inclusive) low cutoff pointjava.lang.ClassCastException
- if from
is not compatible with
the comparator (or is not Comparable, for natural ordering)java.lang.NullPointerException
- if from is null, but the comparator
does not tolerate null elementspublic void $COMMIT(long timestamp)
Rollbackable
$COMMIT
in interface Rollbackable
$COMMIT
in interface Collection
$COMMIT
in interface Set
$COMMIT
in interface SortedSet
$COMMIT
in class AbstractSet
timestamp
- The timestamp.public void $RESTORE(long timestamp, boolean trim)
Rollbackable
$RESTORE
in interface Rollbackable
$RESTORE
in interface Collection
$RESTORE
in interface Set
$RESTORE
in interface SortedSet
$RESTORE
in class AbstractSet
timestamp
- The timestamp taken at the time when the previous
state was recorded.trim
- Whether to delete the records used for the rollback.Checkpoint.rollback(long, boolean)