Package gw.util

Class SpaceEfficientHashMap<K,​V>

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

    public class SpaceEfficientHashMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Object _keys  
      private java.lang.Object _values  
      private static java.lang.Object EMPTY_KEY  
      private static int MAX_LIST_SIZE  
      (package private) int modCount
      The number of times this HashMap has been structurally modified Structural modifications are those that change the number of mappings in the HashMap or otherwise modify its internal structure (e.g., rehash).
    • Constructor Summary

      Constructors 
      Constructor Description
      SpaceEfficientHashMap()  
      SpaceEfficientHashMap​(java.util.Map<? extends K,​? extends V> m)
      Constructs a new HashMap with the same mappings as the specified Map.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      boolean containsKey​(java.lang.Object key)  
      boolean containsValue​(java.lang.Object value)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      boolean equals​(java.lang.Object o)
      Compares the specified object with this map for equality.
      V get​(java.lang.Object key)  
      int hashCode()
      Returns the hash code value for this map.
      boolean isEmpty()  
      private boolean isList()  
      private boolean isMap()  
      private boolean isObject()  
      java.util.Set<K> keySet()  
      V put​(K key, V value)  
      void putAll​(java.util.Map<? extends K,​? extends V> m)  
      V remove​(java.lang.Object key)  
      int size()  
      java.lang.String toString()  
      void trimToSize()  
      java.util.Collection<V> values()  
      • Methods inherited from class java.lang.Object

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

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

      • EMPTY_KEY

        private static final java.lang.Object EMPTY_KEY
      • _keys

        private java.lang.Object _keys
      • _values

        private java.lang.Object _values
      • modCount

        transient volatile int modCount
        The number of times this HashMap has been structurally modified Structural modifications are those that change the number of mappings in the HashMap or otherwise modify its internal structure (e.g., rehash). This field is used to make iterators on Collection-views of the HashMap fail-fast. (See ConcurrentModificationException).
    • Constructor Detail

      • SpaceEfficientHashMap

        public SpaceEfficientHashMap()
      • SpaceEfficientHashMap

        public SpaceEfficientHashMap​(java.util.Map<? extends K,​? extends V> m)
        Constructs a new HashMap with the same mappings as the specified Map. The HashMap is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified Map.
        Parameters:
        m - the map whose mappings are to be placed in this map
        Throws:
        java.lang.NullPointerException - if the specified map is null
    • Method Detail

      • size

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

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

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

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

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

        public V put​(K key,
                     V value)
        Specified by:
        put in interface java.util.Map<K,​V>
      • remove

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

        public void putAll​(java.util.Map<? extends K,​? extends V> m)
        Specified by:
        putAll in interface java.util.Map<K,​V>
      • clear

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

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

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

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

        public boolean equals​(java.lang.Object o)
        Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface.

        This implementation first checks if the specified object is this map; if so it returns true. Then, it checks if the specified object is a map whose size is identical to the size of this map; if not, it returns false. If so, it iterates over this map's entrySet collection, and checks that the specified map contains each mapping that this map contains. If the specified map fails to contain such a mapping, false is returned. If the iteration completes, true is returned.

        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - object to be compared for equality with this map
        Returns:
        true if the specified object is equal to this map
      • hashCode

        public int hashCode()
        Returns the hash code value for this map. The hash code of a map is defined to be the sum of the hash codes of each entry in the map's entrySet() view. This ensures that m1.equals(m2) implies that m1.hashCode()==m2.hashCode() for any two maps m1 and m2, as required by the general contract of Object.hashCode().

        This implementation iterates over entrySet(), calling hashCode() on each element (entry) in the set, and adding up the results.

        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code value for this map
        See Also:
        Map.Entry.hashCode(), Object.equals(Object), Set.equals(Object)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • isObject

        private boolean isObject()
      • isList

        private boolean isList()
      • isMap

        private boolean isMap()
      • trimToSize

        public void trimToSize()