Class SmallMap<K,​V>

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

    public class SmallMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>
    Map implementation with a smallest possible memory usage. It should only be used for maps with small number of items (e.g. <30) since most operations have an O(n) complexity. Thus it should be used in cases with large number of map objects, each having only few items.

    null is not supported for keys or values.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private class  SmallMap.SmallMapEntry  
      • Nested classes/interfaces inherited from interface java.util.Map

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

      Fields 
      Modifier and Type Field Description
      private java.lang.Object[] mapArr
      stores key-value pair as 2 objects; key first; in case of empty map this might be null
    • Constructor Summary

      Constructors 
      Constructor Description
      SmallMap()
      Creates empty map.
      SmallMap​(java.util.Map<? extends K,​? extends V> initMap)
      Creates map filled with entries from provided 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()  
      private int findKey​(java.lang.Object key)
      Returns index of key within map-array or -1 if key is not found (or key is null).
      private int findValue​(java.lang.Object value)
      Returns index of value within map-array or -1 if value is not found (or value is null).
      V get​(java.lang.Object key)  
      boolean isEmpty()  
      java.util.Set<K> keySet()
      Returns a set view of the keys contained in this map.
      V put​(K key, V value)  
      void putAll​(java.util.Map<? extends K,​? extends V> otherMap)  
      V remove​(java.lang.Object key)  
      int size()  
      java.util.Collection<V> values()
      Returns a collection of the values contained in this map.
      • Methods inherited from class java.lang.Object

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

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

      • mapArr

        private java.lang.Object[] mapArr
        stores key-value pair as 2 objects; key first; in case of empty map this might be null
    • Constructor Detail

      • SmallMap

        public SmallMap()
        Creates empty map.
      • SmallMap

        public SmallMap​(java.util.Map<? extends K,​? extends V> initMap)
        Creates map filled with entries from provided map.
    • Method Detail

      • findKey

        private int findKey​(java.lang.Object key)
        Returns index of key within map-array or -1 if key is not found (or key is null).
      • findValue

        private int findValue​(java.lang.Object value)
        Returns index of value within map-array or -1 if value is not found (or value is null).
      • 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 final void putAll​(java.util.Map<? extends K,​? extends V> otherMap)
        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()
        Returns a set view of the keys contained in this map.

        The current implementation does not allow changes to the returned key set (which would have to be reflected in the underlying map.

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

        public java.util.Collection<V> values()
        Returns a collection of the values contained in this map.

        The current implementation does not allow changes to the returned collection (which would have to be reflected in the underlying map.

        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>