Class NullMap<K,​V>

  • All Implemented Interfaces:
    java.io.Serializable, java.util.Map<K,​V>

    public final class NullMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>, java.io.Serializable
    An (immutable) empty Map, that supports all Map operations without throwing exceptions (in contrast to Collections.EMPTY_MAP that will throw exceptions on put/remove).

    NOTE: This is not a general purpose Map implementation, as the put and putAll methods will not modify the map. Instances of this class will always be an empty map.

    Version:
    $Id: com/twelvemonkeys/util/NullMap.java#2 $
    Author:
    Harald Kuhr
    See Also:
    Serialized Form
    • Nested Class Summary

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

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      NullMap()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      boolean containsKey​(java.lang.Object pKey)  
      boolean containsValue​(java.lang.Object pValue)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      boolean equals​(java.lang.Object pOther)
      Tests the given object for equality (wether it is also an empty Map).
      V get​(java.lang.Object pKey)  
      int hashCode()
      Returns the hashCode of the empty map, 0.
      boolean isEmpty()  
      java.util.Set<K> keySet()  
      V put​(java.lang.Object pKey, java.lang.Object pValue)  
      void putAll​(java.util.Map pMap)  
      V remove​(java.lang.Object pKey)  
      int size()  
      java.util.Collection<V> values()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • NullMap

        public NullMap()
    • Method Detail

      • size

        public final int size()
        Specified by:
        size in interface java.util.Map<K,​V>
      • clear

        public final void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
      • isEmpty

        public final boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
      • containsKey

        public final boolean containsKey​(java.lang.Object pKey)
        Specified by:
        containsKey in interface java.util.Map<K,​V>
      • containsValue

        public final boolean containsValue​(java.lang.Object pValue)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
      • values

        public final java.util.Collection<V> values()
        Specified by:
        values in interface java.util.Map<K,​V>
      • putAll

        public final void putAll​(java.util.Map pMap)
        Specified by:
        putAll in interface java.util.Map<K,​V>
      • entrySet

        public final java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
      • keySet

        public final java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
      • get

        public final V get​(java.lang.Object pKey)
        Specified by:
        get in interface java.util.Map<K,​V>
      • remove

        public final V remove​(java.lang.Object pKey)
        Specified by:
        remove in interface java.util.Map<K,​V>
      • put

        public final V put​(java.lang.Object pKey,
                           java.lang.Object pValue)
        Specified by:
        put in interface java.util.Map<K,​V>
      • equals

        public boolean equals​(java.lang.Object pOther)
        Tests the given object for equality (wether it is also an empty Map). This is consistent with the standard Map implementations of the Java Collections Framework.
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.lang.Object
        Parameters:
        pOther - the object to compare with
        Returns:
        true if pOther is an empty Map, otherwise false
      • hashCode

        public int hashCode()
        Returns the hashCode of the empty map, 0. This is consistent with the standard Map implementations of the Java Collections Framework.
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        0, always