FileDocCategorySizeDatePackage
TreeMap.javaAPI DocAndroid 1.5 API49753Wed May 06 22:41:04 BST 2009java.util

TreeMap

public class TreeMap extends AbstractMap implements Serializable, SortedMap, Cloneable
TreeMap is an implementation of SortedMap. All optional operations (adding and removing) are supported. The values can be any objects. The keys 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
transient int
size
transient Entry
root
private Comparator
comparator
transient int
modCount
transient Set
entrySet
Constructors Summary
public TreeMap()
Constructs a new empty {@code TreeMap} instance.

since
Android 1.0

        super();
    
public TreeMap(Comparator comparator)
Constructs a new empty {@code TreeMap} instance with the specified comparator.

param
comparator the comparator to compare keys with.
since
Android 1.0

        this.comparator = comparator;
    
public TreeMap(Map map)
Constructs a new {@code TreeMap} instance containing the mappings from the specified map and using natural ordering.

param
map the mappings to add.
throws
ClassCastException if a key in the specified map does not implement the Comparable interface, or if the keys in the map cannot be compared.
since
Android 1.0

        this();
        putAll(map);
    
public TreeMap(SortedMap map)
Constructs a new {@code TreeMap} instance containing the mappings from the specified SortedMap and using the same comparator.

param
map the mappings to add.
since
Android 1.0

        this(map.comparator());
        Iterator<? extends Map.Entry<K, ? extends V>> it = map.entrySet().iterator();
        if (it.hasNext()) {
            Map.Entry<K, ? extends V> entry = it.next();
            Entry<K, V> last = new Entry<K, V>(entry.getKey(), entry.getValue());
            root = last;
            size = 1;
            while (it.hasNext()) {
                entry = it.next();
                Entry<K, V> x = new Entry<K, V>(entry.getKey(), entry.getValue());
                x.parent = last;
                last.right = x;
                size++;
                balance(x);
                last = x;
            }
        }
    
Methods Summary
voidbalance(java.util.TreeMap$Entry x)

        Entry<K, V> y;
        x.color = true;
        while (x != root && x.parent.color) {
            if (x.parent == x.parent.parent.left) {
                y = x.parent.parent.right;
                if (y != null && y.color) {
                    x.parent.color = false;
                    y.color = false;
                    x.parent.parent.color = true;
                    x = x.parent.parent;
                } else {
                    if (x == x.parent.right) {
                        x = x.parent;
                        leftRotate(x);
                    }
                    x.parent.color = false;
                    x.parent.parent.color = true;
                    rightRotate(x.parent.parent);
                }
            } else {
                y = x.parent.parent.left;
                if (y != null && y.color) {
                    x.parent.color = false;
                    y.color = false;
                    x.parent.parent.color = true;
                    x = x.parent.parent;
                } else {
                    if (x == x.parent.left) {
                        x = x.parent;
                        rightRotate(x);
                    }
                    x.parent.color = false;
                    x.parent.parent.color = true;
                    leftRotate(x.parent.parent);
                }
            }
        }
        root.color = false;
    
public voidclear()
Removes all mappings from this TreeMap, leaving it empty.

see
Map#isEmpty()
see
#size()
since
Android 1.0

        root = null;
        size = 0;
        modCount++;
    
public java.lang.Objectclone()
Returns a new {@code TreeMap} with the same mappings, size and comparator as this instance.

return
a shallow copy of this instance.
see
java.lang.Cloneable
since
Android 1.0

        try {
            TreeMap<K, V> clone = (TreeMap<K, V>) super.clone();
            clone.entrySet = null;
            if (root != null) {
                clone.root = root.clone(null);
            }
            return clone;
        } catch (CloneNotSupportedException e) {
            return null;
        }
    
public java.util.Comparatorcomparator()
Returns the comparator used to compare elements in this map.

return
the comparator or {@code null} if the natural ordering is used.
since
Android 1.0

        return comparator;
    
public booleancontainsKey(java.lang.Object key)
Returns whether this map contains the specified key.

param
key the key to search for.
return
{@code true} if this map contains the specified key, {@code false} otherwise.
since
Android 1.0

        return find(key) != null;
    
public booleancontainsValue(java.lang.Object value)
Returns whether this map contains the specified value.

param
value the value to search for.
return
{@code true} if this map contains the specified value, {@code false} otherwise.
since
Android 1.0

        if (root != null) {
            return containsValue(root, value);
        }
        return false;
    
private booleancontainsValue(java.util.TreeMap$Entry node, java.lang.Object value)

        if (value == null ? node.value == null : value.equals(node.value)) {
            return true;
        }
        if (node.left != null) {
            if (containsValue(node.left, value)) {
                return true;
            }
        }
        if (node.right != null) {
            if (containsValue(node.right, value)) {
                return true;
            }
        }
        return false;
    
public java.util.SetentrySet()
Returns a set containing all of the mappings in this map. Each mapping is an instance of {@link Map.Entry}. As the set is backed by this map, changes in one will be reflected in the other. It does not support adding operations.

return
a set of the mappings.
since
Android 1.0

        if (entrySet == null) {
            entrySet = new AbstractSet<Map.Entry<K, V>>() {
                 @Override
                public int size() {
                    return size;
                }

                @Override
                public void clear() {
                    TreeMap.this.clear();
                }

                @SuppressWarnings("unchecked")
                @Override
                public boolean contains(Object object) {
                    if (object instanceof Map.Entry) {
                        Map.Entry<K, V> entry = (Map.Entry<K, V>) object;
                        Object v1 = get(entry.getKey()), v2 = entry.getValue();
                        return v1 == null ? v2 == null : v1.equals(v2);
                    }
                    return false;
                }

                @Override
                public Iterator<Map.Entry<K, V>> iterator() {
                    return new UnboundedEntryIterator<K, V>(TreeMap.this);
                }
            };
        }
        return entrySet;
    
private java.util.TreeMap$Entryfind(java.lang.Object keyObj)

        int result;
        K key = (K)keyObj;
        Comparable<K> object = null;
        if (comparator == null) {
            object = toComparable(key);
        }
        Entry<K, V> x = root;
        while (x != null) {
            result = object != null ? object.compareTo(x.key) : comparator
                    .compare(key, x.key);
            if (result == 0) {
                return x;
            }
            x = result < 0 ? x.left : x.right;
        }
        return null;
    
java.util.TreeMap$EntryfindAfter(java.lang.Object keyObj)

        K key = (K)keyObj;
        int result;
        Comparable<K> object = null;
        if (comparator == null) {
            object = toComparable(key);
        }
        Entry<K, V> x = root, last = null;
        while (x != null) {
            result = object != null ? object.compareTo(x.key) : comparator
                    .compare(key, x.key);
            if (result == 0) {
                return x;
            }
            if (result < 0) {
                last = x;
                x = x.left;
            } else {
                x = x.right;
            }
        }
        return last;
    
java.util.TreeMap$EntryfindBefore(K key)

        int result;
        Comparable<K> object = null;
        if (comparator == null) {
            object = toComparable(key);
        }
        Entry<K, V> x = root, last = null;
        while (x != null) {
            result = object != null ? object.compareTo(x.key) : comparator
                    .compare(key, x.key);
            if (result <= 0) {
                x = x.left;
            } else {
                last = x;
                x = x.right;
            }
        }
        return last;
    
public KfirstKey()
Returns the first key in this map.

return
the first key in this map.
exception
NoSuchElementException if this sorted map is empty.
since
Android 1.0

        if (root != null) {
            return minimum(root).key;
        }
        throw new NoSuchElementException();
    
private voidfixup(java.util.TreeMap$Entry x)

        Entry<K, V> w;
        while (x != root && !x.color) {
            if (x == x.parent.left) {
                w = x.parent.right;
                if (w == null) {
                    x = x.parent;
                    continue;
                }
                if (w.color) {
                    w.color = false;
                    x.parent.color = true;
                    leftRotate(x.parent);
                    w = x.parent.right;
                    if (w == null) {
                        x = x.parent;
                        continue;
                    }
                }
                if ((w.left == null || !w.left.color)
                        && (w.right == null || !w.right.color)) {
                    w.color = true;
                    x = x.parent;
                } else {
                    if (w.right == null || !w.right.color) {
                        w.left.color = false;
                        w.color = true;
                        rightRotate(w);
                        w = x.parent.right;
                    }
                    w.color = x.parent.color;
                    x.parent.color = false;
                    w.right.color = false;
                    leftRotate(x.parent);
                    x = root;
                }
            } else {
                w = x.parent.left;
                if (w == null) {
                    x = x.parent;
                    continue;
                }
                if (w.color) {
                    w.color = false;
                    x.parent.color = true;
                    rightRotate(x.parent);
                    w = x.parent.left;
                    if (w == null) {
                        x = x.parent;
                        continue;
                    }
                }
                if ((w.left == null || !w.left.color)
                        && (w.right == null || !w.right.color)) {
                    w.color = true;
                    x = x.parent;
                } else {
                    if (w.left == null || !w.left.color) {
                        w.right.color = false;
                        w.color = true;
                        leftRotate(w);
                        w = x.parent.left;
                    }
                    w.color = x.parent.color;
                    x.parent.color = false;
                    w.left.color = false;
                    rightRotate(x.parent);
                    x = root;
                }
            }
        }
        x.color = false;
    
public Vget(java.lang.Object key)
Returns the value of the mapping with the specified key.

param
key the key.
return
the value of the mapping with the specified key.
throws
ClassCastException if the key cannot be compared with the keys in this map.
throws
NullPointerException if the key is {@code null} and the comparator cannot handle {@code null}.
since
Android 1.0

        Entry<K, V> node = find(key);
        if (node != null) {
            return node.value;
        }
        return null;
    
public java.util.SortedMapheadMap(K endKey)
Returns a sorted map over a range of this sorted map with all keys that are less than the specified {@code endKey}. Changes to the returned sorted map are reflected in this sorted map and vice versa.

Note: The returned map will not allow an insertion of a key outside the specified range.

param
endKey the high boundary of the range specified.
return
a sorted map where the keys are less than {@code endKey}.
throws
ClassCastException if the specified key cannot be compared with the keys in this map.
throws
NullPointerException if the specified key is {@code null} and the comparator cannot handle {@code null} keys.
throws
IllegalArgumentException if this map is itself a sorted map over a range of another map and the specified key is outside of its range.
since
Android 1.0

        // Check for errors
        if (comparator == null) {
            toComparable(endKey).compareTo(endKey);
        } else {
            comparator.compare(endKey, endKey);
        }
        return new SubMap<K, V>(this, endKey);
    
public java.util.SetkeySet()
Returns a set of the keys contained in this map. The set is backed by this map so changes to one are reflected by the other. The set does not support adding.

return
a set of the keys.
since
Android 1.0

        if (keySet == null) {
            keySet = new AbstractSet<K>() {
                @Override
                public boolean contains(Object object) {
                    return containsKey(object);
                }

                @Override
                public int size() {
                    return size;
                }

                @Override
                public void clear() {
                    TreeMap.this.clear();
                }

                @Override
                public Iterator<K> iterator() {
                    return new UnboundedKeyIterator<K,V> (TreeMap.this);
                }
            };
        }
        return keySet;
    
public KlastKey()
Returns the last key in this map.

return
the last key in this map.
throws
NoSuchElementException if this map is empty.
since
Android 1.0

        if (root != null) {
            return maximum(root).key;
        }
        throw new NoSuchElementException();
    
private voidleftRotate(java.util.TreeMap$Entry x)

        Entry<K, V> y = x.right;
        x.right = y.left;
        if (y.left != null) {
            y.left.parent = x;
        }
        y.parent = x.parent;
        if (x.parent == null) {
            root = y;
        } else {
            if (x == x.parent.left) {
                x.parent.left = y;
            } else {
                x.parent.right = y;
            }
        }
        y.left = x;
        x.parent = y;
    
static java.util.TreeMap$Entrymaximum(java.util.TreeMap$Entry x)

        while (x.right != null) {
            x = x.right;
        }
        return x;
    
static java.util.TreeMap$Entryminimum(java.util.TreeMap$Entry x)

        while (x.left != null) {
            x = x.left;
        }
        return x;
    
static java.util.TreeMap$Entrypredecessor(java.util.TreeMap$Entry x)

        if (x.left != null) {
            return maximum(x.left);
        }
        Entry<K, V> y = x.parent;
        while (y != null && x == y.left) {
            x = y;
            y = y.parent;
        }
        return y;
    
public Vput(K key, V value)
Maps the specified key to the specified value.

param
key the key.
param
value the value.
return
the value of any previous mapping with the specified key or {@code null} if there was no mapping.
throws
ClassCastException if the specified key cannot be compared with the keys in this map.
throws
NullPointerException if the specified key is {@code null} and the comparator cannot handle {@code null} keys.
since
Android 1.0

        MapEntry<K, V> entry = rbInsert(key);
        V result = entry.value;
        entry.value = value;
        return result;
    
public voidputAll(java.util.Map map)
Copies all the mappings in the given map to this map. These mappings will replace all mappings that this map had for any of the keys currently in the given map.

param
map the map to copy mappings from.
throws
ClassCastException if a key in the specified map cannot be compared with the keys in this map.
throws
NullPointerException if a key in the specified map is {@code null} and the comparator cannot handle {@code null} keys.
since
Android 1.0

        super.putAll(map);
    
voidrbDelete(java.util.TreeMap$Entry z)

        Entry<K, V> y = z.left == null || z.right == null ? z : successor(z);
        Entry<K, V> x = y.left != null ? y.left : y.right;
        if (x != null) {
            x.parent = y.parent;
        }
        if (y.parent == null) {
            root = x;
        } else if (y == y.parent.left) {
            y.parent.left = x;
        } else {
            y.parent.right = x;
        }
        modCount++;
        if (y != z) {
            z.key = y.key;
            z.value = y.value;
        }
        if (!y.color && root != null) {
            if (x == null) {
                fixup(y.parent);
            } else {
                fixup(x);
            }
        }
        size--;
    
private java.util.TreeMap$EntryrbInsert(K object)

        int result = 0;
        Entry<K, V> y = null;
        if (size != 0) {
            Comparable<K> key = null;
            if (comparator == null) {
                key = toComparable(object);
            }
            Entry<K, V> x = root;
            while (x != null) {
                y = x;
                result = key != null ? key.compareTo(x.key) : comparator
                        .compare(object, x.key);
                if (result == 0) {
                    return x;
                }
                x = result < 0 ? x.left : x.right;
            }
        }

        size++;
        modCount++;
        Entry<K, V> z = new Entry<K, V>(object);
        if (y == null) {
            return root = z;
        }
        z.parent = y;
        if (result < 0) {
            y.left = z;
        } else {
            y.right = z;
        }
        balance(z);
        return z;
    
private voidreadObject(java.io.ObjectInputStream stream)

        stream.defaultReadObject();
        size = stream.readInt();
        Entry<K, V> last = null;
        for (int i = size; --i >= 0;) {
            Entry<K, V> node = new Entry<K, V>((K)stream.readObject());
            node.value = (V)stream.readObject();
            if (last == null) {
                root = node;
            } else {
                node.parent = last;
                last.right = node;
                balance(node);
            }
            last = node;
        }
    
public Vremove(java.lang.Object key)
Removes the mapping with the specified key from this map.

param
key the key of the mapping to remove.
return
the value of the removed mapping or {@code null} if no mapping for the specified key was found.
throws
ClassCastException if the specified key cannot be compared with the keys in this map.
throws
NullPointerException if the specified key is {@code null} and the comparator cannot handle {@code null} keys.
since
Android 1.0

        if (size == 0) {
            return null;
        }
        Entry<K, V> node = find(key);
        if (node == null) {
            return null;
        }
        V result = node.value;
        rbDelete(node);
        return result;
    
private voidrightRotate(java.util.TreeMap$Entry x)

        Entry<K, V> y = x.left;
        x.left = y.right;
        if (y.right != null) {
            y.right.parent = x;
        }
        y.parent = x.parent;
        if (x.parent == null) {
            root = y;
        } else {
            if (x == x.parent.right) {
                x.parent.right = y;
            } else {
                x.parent.left = y;
            }
        }
        y.right = x;
        x.parent = y;
    
public intsize()
Returns the number of mappings in this map.

return
the number of mappings in this map.
since
Android 1.0

        return size;
    
public java.util.SortedMapsubMap(K startKey, K endKey)
Returns a sorted map over a range of this sorted map with all keys greater than or equal to the specified {@code startKey} and less than the specified {@code endKey}. Changes to the returned sorted map are reflected in this sorted map and vice versa.

Note: The returned map will not allow an insertion of a key outside the specified range.

param
startKey the low boundary of the range (inclusive).
param
endKey the high boundary of the range (exclusive),
return
a sorted map with the key from the specified range.
throws
ClassCastException if the start or end key cannot be compared with the keys in this map.
throws
NullPointerException if the start or end key is {@code null} and the comparator cannot handle {@code null} keys.
throws
IllegalArgumentException if the start key is greater than the end key, or if this map is itself a sorted map over a range of another sorted map and the specified range is outside of its range.
since
Android 1.0

        if (comparator == null) {
            if (toComparable(startKey).compareTo(endKey) <= 0) {
                return new SubMap<K, V>(startKey, this, endKey);
            }
        } else {
            if (comparator.compare(startKey, endKey) <= 0) {
                return new SubMap<K, V>(startKey, this, endKey);
            }
        }
        throw new IllegalArgumentException();
    
static java.util.TreeMap$Entrysuccessor(java.util.TreeMap$Entry x)

        if (x.right != null) {
            return minimum(x.right);
        }
        Entry<K, V> y = x.parent;
        while (y != null && x == y.right) {
            x = y;
            y = y.parent;
        }
        return y;
    
public java.util.SortedMaptailMap(K startKey)
Returns a sorted map over a range of this sorted map with all keys that are greater than or equal to the specified {@code startKey}. Changes to the returned sorted map are reflected in this sorted map and vice versa.

Note: The returned map will not allow an insertion of a key outside the specified range.

param
startKey the low boundary of the range specified.
return
a sorted map where the keys are greater or equal to {@code startKey}.
throws
ClassCastException if the specified key cannot be compared with the keys in this map.
throws
NullPointerException if the specified key is {@code null} and the comparator cannot handle {@code null} keys.
throws
IllegalArgumentException if this map itself a sorted map over a range of another map and the specified key is outside of its range.
since
Android 1.0

        // Check for errors
        if (comparator == null) {
            toComparable(startKey).compareTo(startKey);
        } else {
            comparator.compare(startKey, startKey);
        }
        return new SubMap<K, V>(startKey, this);
    
private static java.lang.ComparabletoComparable(T obj)

        return (Comparable<T>)obj;
    
public java.util.Collectionvalues()
Returns a collection of the values contained in this map. The collection is backed by this map so changes to one are reflected by the other. The collection supports remove, removeAll, retainAll and clear operations, and it does not support add or addAll operations.

This method returns a collection which is the subclass of AbstractCollection. The iterator method of this subclass returns a "wrapper object" over the iterator of map's entrySet(). The {@code size} method wraps the map's size method and the {@code contains} method wraps the map's containsValue method.

The collection is created when this method is called for the first time and returned in response to all subsequent calls. This method may return different collections when multiple concurrent calls occur, since no synchronization is performed.

return
a collection of the values contained in this map.
since
Android 1.0

        if (valuesCollection == null) {
            valuesCollection = new AbstractCollection<V>() {
                @Override
                public boolean contains(Object object) {
                    return containsValue(object);
                }

                @Override
                public int size() {
                    return size;
                }

                @Override
                public void clear() {
                    TreeMap.this.clear();
                }

                @Override
                public Iterator<V> iterator() {
                    return new UnboundedValueIterator<K,V> (TreeMap.this);
                }
            };
        }
        return valuesCollection;
    
private voidwriteObject(java.io.ObjectOutputStream stream)

        stream.defaultWriteObject();
        stream.writeInt(size);
        if (size > 0) {
            Entry<K, V> node = minimum(root);
            while (node != null) {
                stream.writeObject(node.key);
                stream.writeObject(node.value);
                node = successor(node);
            }
        }