FileDocCategorySizeDatePackage
TreeSet.javaAPI DocAndroid 1.5 API13761Wed May 06 22:41:04 BST 2009java.util

TreeSet

public class TreeSet extends AbstractSet implements Serializable, Cloneable, SortedSet
TreeSet is an implementation of SortedSet. All optional operations (adding and removing) are supported. The elements can be any objects which are comparable to each other either using their natural order or a specified Comparator.
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
private transient SortedMap
backingMap
Constructors Summary
private TreeSet(SortedMap map)


       
        this.backingMap = map;
    
public TreeSet()
Constructs a new empty instance of {@code TreeSet} which uses natural ordering.

since
Android 1.0

        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.

param
collection the collection of elements to add.
throws
ClassCastException when an element in the collection does not implement the Comparable interface, or the elements in the collection cannot be compared.
since
Android 1.0

        this();
        addAll(collection);
    
public TreeSet(Comparator comparator)
Constructs a new empty instance of {@code TreeSet} which uses the specified comparator.

param
comparator the comparator to use.
since
Android 1.0

        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.

param
set the SortedSet of elements to add.
since
Android 1.0

        this(set.comparator());
        Iterator<E> it = set.iterator();
        while (it.hasNext()) {
            add(it.next());
        }
    
Methods Summary
public booleanadd(E object)
Adds the specified object to this {@code TreeSet}.

param
object the object to add.
return
{@code true} when this {@code TreeSet} did not already contain the object, {@code false} otherwise.
throws
ClassCastException when the object cannot be compared with the elements in this {@code TreeSet}.
throws
NullPointerException when the object is null and the comparator cannot handle null.
since
Android 1.0

        return backingMap.put(object, object) == null;
    
public booleanaddAll(java.util.Collection collection)
Adds the objects in the specified collection to this {@code TreeSet}.

param
collection the collection of objects to add.
return
{@code true} if this {@code TreeSet} was modified, {@code false} otherwise.
throws
ClassCastException when an object in the collection cannot be compared with the elements in this {@code TreeSet}.
throws
NullPointerException when an object in the collection is null and the comparator cannot handle null.
since
Android 1.0

        return super.addAll(collection);
    
public voidclear()
Removes all elements from this {@code TreeSet}, leaving it empty.

see
#isEmpty
see
#size
since
Android 1.0

        backingMap.clear();
    
public java.lang.Objectclone()
Returns a new {@code TreeSet} with the same elements, size and comparator as this {@code TreeSet}.

return
a shallow copy of this {@code TreeSet}.
see
java.lang.Cloneable
since
Android 1.0

        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.Comparatorcomparator()
Returns the comparator used to compare elements in this {@code TreeSet}.

return
a Comparator or null if the natural ordering is used
since
Android 1.0

        return backingMap.comparator();
    
public booleancontains(java.lang.Object object)
Searches this {@code TreeSet} for the specified object.

param
object the object to search for.
return
{@code true} if {@code object} is an element of this {@code TreeSet}, {@code false} otherwise.
throws
ClassCastException when the object cannot be compared with the elements in this {@code TreeSet}.
throws
NullPointerException when the object is null and the comparator cannot handle null.
since
Android 1.0

        return backingMap.containsKey(object);
    
public Efirst()
Returns the first element in this {@code TreeSet}.

return
the first element.
throws
NoSuchElementException when this {@code TreeSet} is empty.
since
Android 1.0

        return backingMap.firstKey();
    
public java.util.SortedSetheadSet(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.

param
end the end element.
return
a subset where the elements are less than {@code end}
throws
ClassCastException when the end object cannot be compared with the elements in this {@code TreeSet}.
throws
NullPointerException when the end object is null and the comparator cannot handle null.
since
Android 1.0

        // 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 booleanisEmpty()
Returns true if this {@code TreeSet} has no element, otherwise false.

return
true if this {@code TreeSet} has no element.
see
#size
since
Android 1.0

        return backingMap.isEmpty();
    
public java.util.Iteratoriterator()
Returns an Iterator on the elements of this {@code TreeSet}.

return
an Iterator on the elements of this {@code TreeSet}.
see
Iterator
since
Android 1.0

        return backingMap.keySet().iterator();
    
public Elast()
Returns the last element in this {@code TreeSet}. The last element is the highest element.

return
the last element.
throws
NoSuchElementException when this {@code TreeSet} is empty.
since
Android 1.0

        return backingMap.lastKey();
    
private voidreadObject(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 booleanremove(java.lang.Object object)
Removes an occurrence of the specified object from this {@code TreeSet}.

param
object the object to remove.
return
{@code true} if this {@code TreeSet} was modified, {@code false} otherwise.
throws
ClassCastException when the object cannot be compared with the elements in this {@code TreeSet}.
throws
NullPointerException when the object is null and the comparator cannot handle null.
since
Android 1.0

        return backingMap.remove(object) != null;
    
public intsize()
Returns the number of elements in this {@code TreeSet}.

return
the number of elements in this {@code TreeSet}.
since
Android 1.0

        return backingMap.size();
    
public java.util.SortedSetsubSet(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.

param
start the start element.
param
end the end element (exclusive).
return
a subset where the elements are greater or equal to {@code start} and less than {@code end}
throws
ClassCastException when the start or end object cannot be compared with the elements in this {@code TreeSet}.
throws
NullPointerException when the start or end object is null and the comparator cannot handle null.
since
Android 1.0

        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.SortedSettailSet(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.

param
start the start element.
return
a subset where the elements are greater or equal to {@code start}
throws
ClassCastException when the start object cannot be compared with the elements in this {@code TreeSet}.
throws
NullPointerException when the start object is null and the comparator cannot handle null.
since
Android 1.0

        // 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 voidwriteObject(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());
            }
        }