Package java.util

Class AbstractMap<K,​V>

java.lang.Object
java.util.AbstractMap<K,​V>
All Implemented Interfaces:
Map<K,​V>
Direct Known Subclasses:
ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, IdentityHashMap, TreeMap, WeakHashMap

public abstract class AbstractMap<K,​V>
extends Object
implements Map<K,​V>
A base class for Map implementations.

Subclasses that permit new mappings to be added must override put(K, V).

The default implementations of many methods are inefficient for large maps. For example in the default implementation, each call to get(java.lang.Object) performs a linear iteration of the entry set. Subclasses should override such methods to improve their performance.

Since:
1.2
  • Nested Class Summary

    Nested Classes
    Modifier and Type Class Description
    static class  AbstractMap.SimpleEntry<K,​V>
    A key-value mapping with mutable values.
    static class  AbstractMap.SimpleImmutableEntry<K,​V>
    An immutable key-value mapping.

    Nested classes/interfaces inherited from interface java.util.Map

    Map.Entry<K,​V>
  • Constructor Summary

    Constructors
    Modifier Constructor Description
    protected AbstractMap()  
  • Method Summary

    Modifier and Type Method Description
    void clear()
    Removes all elements from this Map, leaving it empty.
    protected Object clone()
    Creates and returns a copy of this Object.
    boolean containsKey​(Object key)
    Returns whether this Map contains the specified key.
    boolean containsValue​(Object value)
    Returns whether this Map contains the specified value.
    abstract Set<Map.Entry<K,​V>> entrySet()
    Returns a Set containing all of the mappings in this Map.
    boolean equals​(Object object)
    Compares this instance with the specified object and indicates if they are equal.
    V get​(Object key)
    Returns the value of the mapping with the specified key.
    int hashCode()
    Returns an integer hash code for this object.
    boolean isEmpty()
    Returns whether this map is empty.
    Set<K> keySet()
    Returns a set of the keys contained in this Map.
    V put​(K key, V value)
    Maps the specified key to the specified value.
    void putAll​(Map<? extends K,​? extends V> map)
    Copies every mapping in the specified Map to this Map.
    V remove​(Object key)
    Removes a mapping with the specified key from this Map.
    int size()
    Returns the number of mappings in this Map.
    String toString()
    Returns a string containing a concise, human-readable description of this object.
    Collection<V> values()
    Returns a Collection of the values contained in this Map.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • AbstractMap

      protected AbstractMap()
  • Method Details

    • clear

      public void clear()
      Removes all elements from this Map, leaving it empty.

      This implementation calls entrySet().clear().

      Specified by:
      clear in interface Map<K,​V>
      See Also:
      Map.isEmpty(), Map.size()
    • containsKey

      public boolean containsKey​(Object key)
      Returns whether this Map contains the specified key.

      This implementation iterates its key set, looking for a key that key equals.

      Specified by:
      containsKey in interface Map<K,​V>
      Parameters:
      key - the key to search for.
      Returns:
      true if this map contains the specified key, false otherwise.
    • containsValue

      public boolean containsValue​(Object value)
      Returns whether this Map contains the specified value.

      This implementation iterates its entry set, looking for an entry with a value that value equals.

      Specified by:
      containsValue in interface Map<K,​V>
      Parameters:
      value - the value to search for.
      Returns:
      true if this map contains the specified value, false otherwise.
    • entrySet

      public abstract Set<Map.Entry<K,​V>> entrySet()
      Description copied from interface: Map
      Returns a Set containing all of the mappings in this Map. Each mapping is an instance of Map.Entry. As the Set is backed by this Map, changes in one will be reflected in the other.
      Specified by:
      entrySet in interface Map<K,​V>
      Returns:
      a set of the mappings
    • equals

      public boolean equals​(Object object)
      Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.

      The default implementation returns true only if this == o. See Writing a correct equals method if you intend implementing your own equals method.

      The general contract for the equals and Object.hashCode() methods is that if equals returns true for any two objects, then hashCode() must return the same value for these objects. This means that subclasses of Object usually override either both methods or neither of them.

      This implementation first checks the structure of object. If it is not a map or of a different size, this returns false. Otherwise it iterates its own entry set, looking up each entry's key in object. If any value does not equal the other map's value for the same key, this returns false. Otherwise it returns true.

      Specified by:
      equals in interface Map<K,​V>
      Overrides:
      equals in class Object
      Parameters:
      object - the object to compare this instance with.
      Returns:
      true if the specified object is equal to this Object; false otherwise.
      See Also:
      Object.hashCode()
    • get

      public V get​(Object key)
      Returns the value of the mapping with the specified key.

      This implementation iterates its entry set, looking for an entry with a key that key equals.

      Specified by:
      get in interface Map<K,​V>
      Parameters:
      key - the key.
      Returns:
      the value of the mapping with the specified key, or null if no mapping for the specified key is found.
    • hashCode

      public int hashCode()
      Returns an integer hash code for this object. By contract, any two objects for which Object.equals(java.lang.Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

      Note that hash values must not change over time unless information used in equals comparisons also changes.

      See Writing a correct hashCode method if you intend implementing your own hashCode method.

      This implementation iterates its entry set, summing the hashcodes of its entries.

      Specified by:
      hashCode in interface Map<K,​V>
      Overrides:
      hashCode in class Object
      Returns:
      this object's hash code.
      See Also:
      Object.equals(java.lang.Object)
    • isEmpty

      public boolean isEmpty()
      Returns whether this map is empty.

      This implementation compares size() to 0.

      Specified by:
      isEmpty in interface Map<K,​V>
      Returns:
      true if this map has no elements, false otherwise.
      See Also:
      Map.size()
    • keySet

      public Set<K> keySet()
      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.

      This implementation returns a view that calls through this to map. Its iterator transforms this map's entry set iterator to return keys.

      Specified by:
      keySet in interface Map<K,​V>
      Returns:
      a set of the keys.
    • put

      public V put​(K key, V value)
      Maps the specified key to the specified value.

      This base implementation throws UnsupportedOperationException.

      Specified by:
      put in interface Map<K,​V>
      Parameters:
      key - the key.
      value - the value.
      Returns:
      the value of any previous mapping with the specified key or null if there was no mapping.
    • putAll

      public void putAll​(Map<? extends K,​? extends V> map)
      Copies every mapping in the specified Map to this Map.

      This implementation iterates through map's entry set, calling put() for each.

      Specified by:
      putAll in interface Map<K,​V>
      Parameters:
      map - the Map to copy mappings from.
    • remove

      public V remove​(Object key)
      Removes a mapping with the specified key from this Map.

      This implementation iterates its entry set, removing the entry with a key that key equals.

      Specified by:
      remove in interface Map<K,​V>
      Parameters:
      key - the key of the mapping to remove.
      Returns:
      the value of the removed mapping or null if no mapping for the specified key was found.
    • size

      public int size()
      Returns the number of mappings in this Map.

      This implementation returns its entry set's size.

      Specified by:
      size in interface Map<K,​V>
      Returns:
      the number of mappings in this Map.
    • toString

      public String toString()
      Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:
         getClass().getName() + '@' + Integer.toHexString(hashCode())

      See Writing a useful toString method if you intend implementing your own toString method.

      This implementation composes a string by iterating its entry set. If this map contains itself as a key or a value, the string "(this Map)" will appear in its place.

      Overrides:
      toString in class Object
      Returns:
      a printable representation of this object.
    • values

      public Collection<V> values()
      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 Collection.remove(java.lang.Object), Collection.removeAll(java.util.Collection<?>), Collection.retainAll(java.util.Collection<?>), and Collection.clear() operations, and it does not support Collection.add(E) or Collection.addAll(java.util.Collection<? extends E>) operations.

      This method returns a Collection which is the subclass of AbstractCollection. The AbstractCollection.iterator() method of this subclass returns a "wrapper object" over the iterator of this Map's Map.entrySet(). The AbstractCollection.size() method wraps this Map's Map.size() method and the AbstractCollection.contains(java.lang.Object) method wraps this Map's Map.containsValue(java.lang.Object) method.

      The collection is created when this method is called at first time and returned in response to all subsequent calls. This method may return different Collection when multiple calls to this method, since it has no synchronization performed.

      This implementation returns a view that calls through this to map. Its iterator transforms this map's entry set iterator to return values.

      Specified by:
      values in interface Map<K,​V>
      Returns:
      a collection of the values contained in this map.
    • clone

      protected Object clone() throws CloneNotSupportedException
      Description copied from class: Object
      Creates and returns a copy of this Object. The default implementation returns a so-called "shallow" copy: It creates a new instance of the same class and then copies the field values (including object references) from this instance to the new instance. A "deep" copy, in contrast, would also recursively clone nested objects. A subclass that needs to implement this kind of cloning should call super.clone() to create the new instance and then create deep copies of the nested, mutable objects.
      Overrides:
      clone in class Object
      Returns:
      a copy of this object.
      Throws:
      CloneNotSupportedException - if this object's class does not implement the Cloneable interface.