FileDocCategorySizeDatePackage
AbstractMap.javaAPI DocAndroid 1.5 API15395Wed May 06 22:41:04 BST 2009java.util

AbstractMap

public abstract class AbstractMap extends Object implements Map
This class is an abstract implementation of the {@code Map} interface. This implementation does not support adding. A subclass must implement the abstract method entrySet().
since
Android 1.0

Fields Summary
Set
keySet
Collection
valuesCollection
Constructors Summary
protected AbstractMap()
Constructs a new instance of this {@code AbstractMap}.

since
Android 1.0

        super();
    
Methods Summary
public voidclear()
Removes all elements from this map, leaving it empty.

throws
UnsupportedOperationException if removing from this map is not supported.
see
#isEmpty()
see
#size()
since
Android 1.0

        entrySet().clear();
    
protected java.lang.Objectclone()
Returns a new instance of the same class as this instance, whose slots have been filled in with the values of the slots of this instance.

return
a shallow copy of this object.
throws
CloneNotSupportedException if the receiver's class does not implement the interface {@code Cloneable}.
since
Android 1.0

        AbstractMap<K, V> result = (AbstractMap<K, V>) super.clone();
        result.keySet = null;
        result.valuesCollection = null;
        return result;
    
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

        Iterator<Map.Entry<K, V>> it = entrySet().iterator();
        if (key != null) {
            while (it.hasNext()) {
                if (key.equals(it.next().getKey())) {
                    return true;
                }
            }
        } else {
            while (it.hasNext()) {
                if (it.next().getKey() == null) {
                    return true;
                }
            }
        }
        return false;
    
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

        Iterator<Map.Entry<K, V>> it = entrySet().iterator();
        if (value != null) {
            while (it.hasNext()) {
                if (value.equals(it.next().getValue())) {
                    return true;
                }
            }
        } else {
            while (it.hasNext()) {
                if (it.next().getValue() == null) {
                    return true;
                }
            }
        }
        return false;
    
public abstract 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.

return
a set of the mappings.
since
Android 1.0

public booleanequals(java.lang.Object object)
Compares the specified object to this instance, and returns {@code true} if the specified object is a map and both maps contain the same mappings.

param
object the object to compare with this object.
return
boolean {@code true} if the object is the same as this object, and {@code false} if it is different from this object.
see
#hashCode()
see
#entrySet()
since
Android 1.0

        if (this == object) {
            return true;
        }
        if (object instanceof Map) {
            Map<?, ?> map = (Map<?, ?>) object;
            if (size() != map.size()) {
                return false;
            }

            // BEGIN android-changed
            // copied from newer version of harmony
            Iterator<Map.Entry<K, V>> it = entrySet().iterator();
            while (it.hasNext()) {
                Entry<K, V> entry = it.next();
                K key = entry.getKey();
                V value = entry.getValue();
                Object obj = map.get(key);
                if( null != obj && (!obj.equals(value)) || null == obj && obj != value) {
                    return false;
                }
            }
            // END android-changed
            return true;
        }
        return 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, or {@code null} if no mapping for the specified key is found.
since
Android 1.0

        Iterator<Map.Entry<K, V>> it = entrySet().iterator();
        if (key != null) {
            while (it.hasNext()) {
                Map.Entry<K, V> entry = it.next();
                if (key.equals(entry.getKey())) {
                    return entry.getValue();
                }
            }
        } else {
            while (it.hasNext()) {
                Map.Entry<K, V> entry = it.next();
                if (entry.getKey() == null) {
                    return entry.getValue();
                }
            }
        }
        return null;
    
public inthashCode()
Returns the hash code for this object. Objects which are equal must return the same value for this method.

return
the hash code of this object.
see
#equals(Object)
since
Android 1.0

        int result = 0;
        Iterator<Map.Entry<K, V>> it = entrySet().iterator();
        while (it.hasNext()) {
            result += it.next().hashCode();
        }
        return result;
    
public booleanisEmpty()
Returns whether this map is empty.

return
{@code true} if this map has no elements, {@code false} otherwise.
see
#size()
since
Android 1.0

        return size() == 0;
    
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 returned 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 AbstractMap.this.size();
                }

                @Override
                public Iterator<K> iterator() {
                    return new Iterator<K>() {
                        Iterator<Map.Entry<K, V>> setIterator = entrySet()
                                .iterator();

                        public boolean hasNext() {
                            return setIterator.hasNext();
                        }

                        public K next() {
                            return setIterator.next().getKey();
                        }

                        public void remove() {
                            setIterator.remove();
                        }
                    };
                }
            };
        }
        return keySet;
    
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
UnsupportedOperationException if adding to this map is not supported.
throws
ClassCastException if the class of the key or value is inappropriate for this map.
throws
IllegalArgumentException if the key or value cannot be added to this map.
throws
NullPointerException if the key or value is {@code null} and this Map does not support {@code null} keys or values.
since
Android 1.0

        throw new UnsupportedOperationException();
    
public voidputAll(java.util.Map map)
Copies every mapping in the specified map to this map.

param
map the map to copy mappings from.
throws
UnsupportedOperationException if adding to this map is not supported.
throws
ClassCastException if the class of a key or value is inappropriate for this map.
throws
IllegalArgumentException if a key or value cannot be added to this map.
throws
NullPointerException if a key or value is {@code null} and this map does not support {@code null} keys or values.
since
Android 1.0

        for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
            put(entry.getKey(), entry.getValue());
        }
    
public Vremove(java.lang.Object key)
Removes a 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
UnsupportedOperationException if removing from this map is not supported.
since
Android 1.0

        Iterator<Map.Entry<K, V>> it = entrySet().iterator();
        if (key != null) {
            while (it.hasNext()) {
                Map.Entry<K, V> entry = it.next();
                if (key.equals(entry.getKey())) {
                    it.remove();
                    return entry.getValue();
                }
            }
        } else {
            while (it.hasNext()) {
                Map.Entry<K, V> entry = it.next();
                if (entry.getKey() == null) {
                    it.remove();
                    return entry.getValue();
                }
            }
        }
        return null;
    
public intsize()
Returns the number of elements in this map.

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

        return entrySet().size();
    
public java.lang.StringtoString()
Returns the string representation of this map.

return
the string representation of this map.
since
Android 1.0

        if (isEmpty()) {
            return "{}"; //$NON-NLS-1$
        }

        StringBuilder buffer = new StringBuilder(size() * 28);
        buffer.append('{");
        Iterator<Map.Entry<K, V>> it = entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<K, V> entry = it.next();
            Object key = entry.getKey();
            if (key != this) {
                buffer.append(key);
            } else {
                buffer.append("(this Map)"); //$NON-NLS-1$
            }
            buffer.append('=");
            Object value = entry.getValue();
            if (value != this) {
                buffer.append(value);
            } else {
                buffer.append("(this Map)"); //$NON-NLS-1$
            }
            if (it.hasNext()) {
                buffer.append(", "); //$NON-NLS-1$
            }
        }
        buffer.append('}");
        return buffer.toString();
    
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 to this method, 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 int size() {
                    return AbstractMap.this.size();
                }

                @Override
                public boolean contains(Object object) {
                    return containsValue(object);
                }

                @Override
                public Iterator<V> iterator() {
                    return new Iterator<V>() {
                        Iterator<Map.Entry<K, V>> setIterator = entrySet()
                                .iterator();

                        public boolean hasNext() {
                            return setIterator.hasNext();
                        }

                        public V next() {
                            return setIterator.next().getValue();
                        }

                        public void remove() {
                            setIterator.remove();
                        }
                    };
                }
            };
        }
        return valuesCollection;