java.lang.Objectjava.util.AbstractCollection<E>
java.util.AbstractSet<E>
AVLSet<E>
public class AVLSet<E>
This class implements the SortedSet interface, backed by an AVL tree.
This class guarantees that the sorted set will be in ascending element order,
sorted according to the natural order of
the elements (see ),
or by the comparator provided at set creation time, depending on which constructor is used.
Comparable
This implementation provides guaranteed log(n) time cost for the operations add,
remove and contains.
Note that the ordering maintained by the set (whether or not an explicit comparator is provided)
must be consistent with equals because the Set interface is
defined in terms of
the equals operation, but an AVLSet instance performs all key
comparisons using its compareTo (or compare) method.
| Constructor Summary | |
|---|---|
AVLSet()
Constructs a new, empty set, sorted according to the elements' natural order. |
|
AVLSet(Collection<? extends E> coll)
Constructs a new set containing the elements in the specified collection, sorted according to the elements' natural order. |
|
AVLSet(Comparator<? super E> comp)
Constructs a new, empty set, sorted according to the specified comparator. |
|
AVLSet(SortedSet<E> set)
Constructs a new set containing the same elements as the specified sorted set, sorted according to the same ordering as the specified set. |
|
| Method Summary | |
|---|---|
boolean |
add(E o)
Adds the specified element to this set if it is not already present. |
boolean |
addAll(Collection<? extends E> coll)
Adds all of the elements in the specified collection to this set. |
void |
clear()
Removes all of the elements from this set. |
Comparator<? super E> |
comparator()
Returns the comparator associated with this sorted set, or null if it uses its elements' natural ordering. |
boolean |
contains(Object o)
Returns true if this set contains the specified element. |
E |
first()
Returns the first (lowest) element currently in this sorted set. |
SortedSet<E> |
headSet(E toElement)
Returns a view of the portion of this set whose elements are strictly less than toElement. |
boolean |
isEmpty()
Returns true if this set contains no elements. |
Iterator<E> |
iterator()
Returns an iterator over the elements in this set. |
E |
last()
Returns the last (highest) element currently in this sorted set. |
boolean |
remove(Object o)
Removes the specified element from this set if it is present. |
int |
size()
Returns the number of elements in this set (its cardinality). |
SortedSet<E> |
subSet(E fromElement,
E toElement)
Returns a view of the portion of this set whose elements range from fromElement,
inclusive, to toElement, exclusive. |
SortedSet<E> |
tailSet(E fromElement)
Returns a view of the portion of this sorted set whose elements are greater than or equal to fromElement. |
| Methods inherited from class java.util.AbstractSet |
|---|
equals, hashCode, removeAll |
| Methods inherited from class java.util.AbstractCollection |
|---|
add, addAll, containsAll, retainAll, toArray, toArray, toString |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface java.util.SortedSet |
|---|
headSet, subSet, tailSet |
| Methods inherited from interface java.util.Set |
|---|
add, addAll, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray |
| Constructor Detail |
|---|
public AVLSet()
public AVLSet(Collection<? extends E> coll)
Comparable interface.
All such keys must be mutually comparable:
k1.compareTo(k2) must not throw a ClassCastException
for any elements k1 and k2 in the set.
coll - The elements that will comprise the new set.
ClassCastException - if the keys in the specified collection are not comparable, or are not mutually comparable.
NullPointerException - - if the specified collection is null.Comparable,
Collectionpublic AVLSet(Comparator<? super E> comp)
comparator.compare(e1, e2) must not throw a ClassCastException
for any elements e2 in the set.
If the user attempts to add an element to the set that violates this constraint,
the add(Object) call will throw a ClassCastException.
comp - the comparator that will be used to sort this set. A null value indicates that the elements' natural ordering should be used.public AVLSet(SortedSet<E> set)
set - the sorted set whose elements will comprise the new set.
NullPointerException - if the specified sorted set is null.| Method Detail |
|---|
public boolean add(E o)
o - the element to be added to this set
true if the set did not already contain the specified element
ClassCastException - if the specified object cannot be compared with the elements currently in the setpublic boolean addAll(Collection<? extends E> coll)
coll - the elements to be added
trueif this set changed as a result of the call
ClassCastException - if the specified object cannot be compared with the elements currently in the set
NullPointerException - if the specified collection is nullpublic void clear()
clear in interface Collectionclear in interface Setclear in class AbstractCollectionpublic Comparator<? super E> comparator()
null if it uses its elements' natural ordering.
comparator in interface SortedSetnull if natural ordering is usedpublic boolean contains(Object o)
true if this set contains the specified element.
contains in interface Collectioncontains in interface Setcontains in class AbstractCollectiono - the object to be checked for membership in this set.
true if the specified element is in this set
ClassCastException - if the specified object cannot be compared with the elements currently in the setpublic E first()
first in interface SortedSetNoSuchElementException - if the sorted set is emptypublic SortedSet<E> headSet(E toElement)
toElement.
The returned sorted set is backed by this set,
so changes in the returned sorted set are reflected in this set,
and vice-versa.
The returned sorted set supports all optional set operations.
toElement - high endpoint (exclusive) of the headSet
toElement
ClassCastException - if toElement is not compatible with this set's comparator,
or, if the set has no comparator, if toElement does not implement Comparable.
IllegalArgumentException - if this set is itself a subSet, headSet, or tailSet,
and toElement is not within the specified range of the subSet, headSet, or tailSet
NullPointerException - if toElement is null
and this set does not tolerate null elementspublic boolean isEmpty()
true if this set contains no elements.
isEmpty in interface CollectionisEmpty in interface SetisEmpty in class AbstractCollectiontrue if no elements in this setpublic Iterator<E> iterator()
iterator in interface Iterableiterator in interface Collectioniterator in interface Setiterator in class AbstractCollectionpublic E last()
last in interface SortedSetNoSuchElementException - if the sorted set is emptypublic boolean remove(Object o)
remove in interface Collectionremove in interface Setremove in class AbstractCollectiono - object to be removed from this set, if present
true if the set contained the specified element
ClassCastException - if the specified object cannot be compared with the elements currently in the setpublic int size()
size in interface Collectionsize in interface Setsize in class AbstractCollection
public SortedSet<E> subSet(E fromElement,
E toElement)
fromElement,
inclusive, to toElement, exclusive.
(If fromElement and toElement are equal,
the returned sorted set is empty.)
The returned sorted set is backed by this set,
so changes in the returned sorted set are reflected in this set, and vice-versa.
fromElement - low endpoint (inclusive) of the subSettoElement - high endpoint (exclusive) of the subSet
fromElement to toElement
ClassCastException - if fromElement and toElement cannot be compared to one another
IllegalArgumentException - if fromElement is greater than toElement
NullPointerException - if fromElement or toElement is null
and this set does not tolerate null elementspublic SortedSet<E> tailSet(E fromElement)
fromElement.
The returned sorted set is backed by this sorted set,
so changes in the returned sorted set are reflected in this sorted set, and vice-versa.
The returned sorted set supports all optional set operations.
fromElement - low endpoint (inclusive) of the subSet
ClassCastException - if fromElement is not compatible with this set's comparator,
or, if the set has no comparator, if toElement does not implement Comparable.
IllegalArgumentException - if this set is itself a subSet, headSet, or tailSet,
and fromElement is not within the specified range of the subSet, headSet, or tailSet
NullPointerException - if fromElement is null
and this set does not tolerate null elements