Constructors Summary |
---|
private TreeSet(SortedMap map)
this.backingMap = map;
|
public TreeSet()Constructs a new empty instance of {@code TreeSet} which uses natural
ordering.
backingMap = new TreeMap<E, E>();
|
public TreeSet(Collection collection)Constructs a new instance of {@code TreeSet} which uses natural ordering
and containing the unique elements in the specified collection.
this();
addAll(collection);
|
public TreeSet(Comparator comparator)Constructs a new empty instance of {@code TreeSet} which uses the
specified comparator.
backingMap = new TreeMap<E, E>(comparator);
|
public TreeSet(SortedSet set)Constructs a new instance of {@code TreeSet} containing the elements of
the specified SortedSet and using the same Comparator.
this(set.comparator());
Iterator<E> it = set.iterator();
while (it.hasNext()) {
add(it.next());
}
|
Methods Summary |
---|
public boolean | add(E object)Adds the specified object to this {@code TreeSet}.
return backingMap.put(object, object) == null;
|
public boolean | addAll(java.util.Collection collection)Adds the objects in the specified collection to this {@code TreeSet}.
return super.addAll(collection);
|
public void | clear()Removes all elements from this {@code TreeSet}, leaving it empty.
backingMap.clear();
|
public java.lang.Object | clone()Returns a new {@code TreeSet} with the same elements, size and comparator
as this {@code TreeSet}.
try {
TreeSet<E> clone = (TreeSet<E>) super.clone();
if (backingMap instanceof TreeMap) {
clone.backingMap = (SortedMap<E, E>) ((TreeMap<E, E>) backingMap).clone();
} else {
clone.backingMap = new TreeMap<E, E>(backingMap);
}
return clone;
} catch (CloneNotSupportedException e) {
return null;
}
|
public java.util.Comparator | comparator()Returns the comparator used to compare elements in this {@code TreeSet}.
return backingMap.comparator();
|
public boolean | contains(java.lang.Object object)Searches this {@code TreeSet} for the specified object.
return backingMap.containsKey(object);
|
public E | first()Returns the first element in this {@code TreeSet}.
return backingMap.firstKey();
|
public java.util.SortedSet | headSet(E end)Returns a SortedSet of the specified portion of this {@code TreeSet}
which contains elements which are all less than the end element. The
returned SortedSet is backed by this {@code TreeSet} so changes to one
are reflected by the other.
// Check for errors
Comparator<? super E> c = backingMap.comparator();
if (c == null) {
((Comparable<E>) end).compareTo(end);
} else {
c.compare(end, end);
}
return new TreeSet<E>(backingMap.headMap(end));
|
public boolean | isEmpty()Returns true if this {@code TreeSet} has no element, otherwise false.
return backingMap.isEmpty();
|
public java.util.Iterator | iterator()Returns an Iterator on the elements of this {@code TreeSet}.
return backingMap.keySet().iterator();
|
public E | last()Returns the last element in this {@code TreeSet}. The last element is
the highest element.
return backingMap.lastKey();
|
private void | readObject(java.io.ObjectInputStream stream)
stream.defaultReadObject();
TreeMap<E, E> map = new TreeMap<E, E>((Comparator<? super E>) stream.readObject());
int size = stream.readInt();
if (size > 0) {
E key = (E)stream.readObject();
TreeMap.Entry<E,E> last = new TreeMap.Entry<E,E>(key,key);
map.root = last;
map.size = 1;
for (int i=1; i<size; i++) {
key = (E)stream.readObject();
TreeMap.Entry<E,E> x = new TreeMap.Entry<E,E>(key,key);
x.parent = last;
last.right = x;
map.size++;
map.balance(x);
last = x;
}
}
backingMap = map;
|
public boolean | remove(java.lang.Object object)Removes an occurrence of the specified object from this {@code TreeSet}.
return backingMap.remove(object) != null;
|
public int | size()Returns the number of elements in this {@code TreeSet}.
return backingMap.size();
|
public java.util.SortedSet | subSet(E start, E end)Returns a SortedSet of the specified portion of this {@code TreeSet}
which contains elements greater or equal to the start element but less
than the end element. The returned SortedSet is backed by this
{@code TreeSet} so changes to one are reflected by the other.
Comparator<? super E> c = backingMap.comparator();
if (c == null) {
if (((Comparable<E>) start).compareTo(end) <= 0) {
return new TreeSet<E>(backingMap.subMap(start, end));
}
} else {
if (c.compare(start, end) <= 0) {
return new TreeSet<E>(backingMap.subMap(start, end));
}
}
throw new IllegalArgumentException();
|
public java.util.SortedSet | tailSet(E start)Returns a SortedSet of the specified portion of this {@code TreeSet}
which contains elements greater or equal to the start element. The
returned SortedSet is backed by this {@code TreeSet} so changes to one
are reflected by the other.
// Check for errors
Comparator<? super E> c = backingMap.comparator();
if (c == null) {
((Comparable<E>) start).compareTo(start);
} else {
c.compare(start, start);
}
return new TreeSet<E>(backingMap.tailMap(start));
|
private void | writeObject(java.io.ObjectOutputStream stream)
stream.defaultWriteObject();
stream.writeObject(backingMap.comparator());
int size = backingMap.size();
stream.writeInt(size);
if (size > 0) {
Iterator<E> it = backingMap.keySet().iterator();
while (it.hasNext()) {
stream.writeObject(it.next());
}
}
|