Class ZHashMap<K,​V>

java.lang.Object
java.util.AbstractMap<K,​V>
java.util.HashMap<K,​V>
org.spiderwiz.zutils.ZHashMap<K,​V>
Type Parameters:
K - type of key
V - type of value
All Implemented Interfaces:
Serializable, Cloneable, Map<K,​V>
Direct Known Subclasses:
ZDictionary

public class ZHashMap<K,​V>
extends HashMap<K,​V>
A synchronized implementation of HashMap.

The Java implementation of HashMap is unsynchronized. This class extends the implementation to provide synchronization.

See Also:
Serialized Form
  • Constructor Details

    • ZHashMap

      public ZHashMap()
      Constructs an empty ZHashMap with the default initial capacity (16) and the default load factor (0.75).
    • ZHashMap

      public ZHashMap​(Map<? extends K,​? extends V> m)
      Constructs a new ZHashMap with the same mappings as the specified Map
      Parameters:
      m - the map whose mappings are to be placed in this map.
  • Method Details

    • lockRead

      public final void lockRead()
      Locks the map for reading.

      This method is used by the class internally to provide synchronization, but you can use it directly when you need to perform a synchronized bulk read operation on the map.

    • lockWrite

      public final void lockWrite()
      Locks the map for writing.

      This method is used by the class internally to provide synchronization, but you can use it directly when you need to perform a synchronized bulk write operation on the map.

    • unlockRead

      public final void unlockRead()
      Unlocks the map after reading.

      This method is used by the class internally, but you should use it directly if you use lockRead() directly.

    • unlockWrite

      public final void unlockWrite()
      Unlocks the map after writing.

      This method is used by the class internally, but you should use it directly if you use lockWrite() directly.

    • get

      public V get​(Object key)
      Synchronized implementation of HashMap.get(java.lang.Object).
      Specified by:
      get in interface Map<K,​V>
      Overrides:
      get in class HashMap<K,​V>
      Parameters:
      key - the key whose associated value is to be returned.
      Returns:
      the value to which the specified key is mapped, or null if this map contains no mapping for the key.
    • put

      public V put​(K key, V value)
      Synchronized implementation of HashMap.put(java.lang.Object, java.lang.Object).
      Specified by:
      put in interface Map<K,​V>
      Overrides:
      put in class HashMap<K,​V>
      Parameters:
      key - key with which the specified value is to be associated.
      value - value to be associated with the specified key.
      Returns:
      the previous value associated with key, or null if there was no mapping for key.
    • putAll

      public void putAll​(Map<? extends K,​? extends V> m)
      Synchronized implementation of HashMap.putAll(java.util.Map).
      Specified by:
      putAll in interface Map<K,​V>
      Overrides:
      putAll in class HashMap<K,​V>
      Parameters:
      m - mappings to be stored in this map.
    • putIfAbsent

      public V putIfAbsent​(K key, V value)
      Specified by:
      putIfAbsent in interface Map<K,​V>
      Overrides:
      putIfAbsent in class HashMap<K,​V>
      Parameters:
      key - key with which the specified value is to be associated
      value - value to be associated with the specified key
      Returns:
      the previous value associated with the specified key, or null if there was no mapping for the key. (A null return can also indicate that the map previously associated null with the key, if the implementation supports null values.)
    • putIfAbsentReturnNew

      public V putIfAbsentReturnNew​(K key, V value)
      This does what HashMap.putIfAbsent(java.lang.Object, java.lang.Object) does (plus synchronization), but it always returns the newly stored value.
      Parameters:
      key - key with which the specified value is to be associated.
      value - value to be associated with the specified key.
      Returns:
      the newly stored value.
    • remove

      public V remove​(Object key)
      Synchronized implementation of HashMap.remove(java.lang.Object).
      Specified by:
      remove in interface Map<K,​V>
      Overrides:
      remove in class HashMap<K,​V>
      Parameters:
      key - key whose mapping is to be removed from the map.
      Returns:
      the previous value associated with key, or null if there was no mapping for key.
    • removeAll

      public boolean removeAll​(Collection<V> c)
      Removes a collection of values from the map.
      Parameters:
      c - collection containing values to be removed from this map.
      Returns:
      true if this map changed as a result of the call.
    • size

      public int size()
      Synchronized implementation of HashMap.size().
      Specified by:
      size in interface Map<K,​V>
      Overrides:
      size in class HashMap<K,​V>
      Returns:
      the number of key-value mappings in this map.
    • containsKey

      public boolean containsKey​(Object key)
      Synchronized implementation of HashMap.containsKey(java.lang.Object).
      Specified by:
      containsKey in interface Map<K,​V>
      Overrides:
      containsKey in class HashMap<K,​V>
      Parameters:
      key - the key whose presence in this map is to be tested.
      Returns:
      true if this map contains a mapping for the specified key.
    • clear

      public void clear()
      Synchronized implementation of HashMap.clear().
      Specified by:
      clear in interface Map<K,​V>
      Overrides:
      clear in class HashMap<K,​V>