Class Flat3Map<K,​V>

  • Type Parameters:
    K - the type of the keys in this map
    V - the type of the values in this map
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.util.Map<K,​V>, Get<K,​V>, IterableGet<K,​V>, IterableMap<K,​V>, Put<K,​V>

    public class Flat3Map<K,​V>
    extends java.lang.Object
    implements IterableMap<K,​V>, java.io.Serializable, java.lang.Cloneable
    A Map implementation that stores data in simple fields until the size is greater than 3.

    This map is designed for performance and can outstrip HashMap. It also has good garbage collection characteristics.

    • Optimised for operation at size 3 or less.
    • Still works well once size 3 exceeded.
    • Gets at size 3 or less are about 0-10% faster than HashMap,
    • Puts at size 3 or less are over 4 times faster than HashMap.
    • Performance 5% slower than HashMap once size 3 exceeded once.

    The design uses two distinct modes of operation - flat and delegate. While the map is size 3 or less, operations map straight onto fields using switch statements. Once size 4 is reached, the map switches to delegate mode and only switches back when cleared. In delegate mode, all operations are forwarded straight to a HashMap resulting in the 5% performance loss.

    The performance gains on puts are due to not needing to create a Map Entry object. This is a large saving not only in performance but in garbage collection.

    Whilst in flat mode this map is also easy for the garbage collector to dispatch. This is because it contains no complex objects or arrays which slow the progress.

    Do not use Flat3Map if the size is likely to grow beyond 3.

    Note that Flat3Map is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. The simplest approach is to wrap this map using Collections.synchronizedMap(Map). This class may throw exceptions when accessed by concurrent threads without synchronization.

    Since:
    3.0
    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
      Flat3Map()
      Constructor.
      Flat3Map​(java.util.Map<? extends K,​? extends V> map)
      Constructor copying elements from another map.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues.
      Flat3Map<K,​V> clone()
      Clones the map without cloning the keys or values.
      boolean containsKey​(java.lang.Object key)
      Checks whether the map contains the specified key.
      boolean containsValue​(java.lang.Object value)
      Checks whether the map contains the specified value.
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      Gets the entrySet view of the map.
      boolean equals​(java.lang.Object obj)
      Compares this map with another.
      V get​(java.lang.Object key)
      Gets the value mapped to the key specified.
      int hashCode()
      Gets the standard Map hashCode.
      boolean isEmpty()
      Checks whether the map is currently empty.
      java.util.Set<K> keySet()
      Gets the keySet view of the map.
      MapIterator<K,​V> mapIterator()
      Gets an iterator over the map.
      V put​(K key, V value)
      Puts a key-value mapping into this map.
      void putAll​(java.util.Map<? extends K,​? extends V> map)
      Puts all the values from the specified map into this map.
      V remove​(java.lang.Object key)
      Removes the specified mapping from this map.
      int size()
      Gets the size of the map.
      java.lang.String toString()
      Gets the map as a String.
      java.util.Collection<V> values()
      Gets the values view of the map.
      • Methods inherited from class java.lang.Object

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

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

      • Flat3Map

        public Flat3Map()
        Constructor.
      • Flat3Map

        public Flat3Map​(java.util.Map<? extends K,​? extends V> map)
        Constructor copying elements from another map.
        Parameters:
        map - the map to copy
        Throws:
        java.lang.NullPointerException - if the map is null
    • Method Detail

      • get

        public V get​(java.lang.Object key)
        Gets the value mapped to the key specified.
        Specified by:
        get in interface Get<K,​V>
        Specified by:
        get in interface java.util.Map<K,​V>
        Parameters:
        key - the key
        Returns:
        the mapped value, null if no match
        See Also:
        Map.get(Object)
      • size

        public int size()
        Gets the size of the map.
        Specified by:
        size in interface Get<K,​V>
        Specified by:
        size in interface java.util.Map<K,​V>
        Returns:
        the size
        See Also:
        Map.size()
      • isEmpty

        public boolean isEmpty()
        Checks whether the map is currently empty.
        Specified by:
        isEmpty in interface Get<K,​V>
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Returns:
        true if the map is currently size zero
        See Also:
        Map.isEmpty()
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Checks whether the map contains the specified key.
        Specified by:
        containsKey in interface Get<K,​V>
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Parameters:
        key - the key to search for
        Returns:
        true if the map contains the key
        See Also:
        Map.containsKey(Object)
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Checks whether the map contains the specified value.
        Specified by:
        containsValue in interface Get<K,​V>
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Parameters:
        value - the value to search for
        Returns:
        true if the map contains the key
        See Also:
        Map.containsValue(Object)
      • put

        public V put​(K key,
                     V value)
        Puts a key-value mapping into this map.
        Specified by:
        put in interface java.util.Map<K,​V>
        Specified by:
        put in interface Put<K,​V>
        Parameters:
        key - the key to add
        value - the value to add
        Returns:
        the value previously mapped to this key, null if none
        See Also:
        Map.put(Object, Object)
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> map)
        Puts all the values from the specified map into this map.
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Specified by:
        putAll in interface Put<K,​V>
        Parameters:
        map - the map to add
        Throws:
        java.lang.NullPointerException - if the map is null
        See Also:
        Map.putAll(Map)
      • remove

        public V remove​(java.lang.Object key)
        Removes the specified mapping from this map.
        Specified by:
        remove in interface Get<K,​V>
        Specified by:
        remove in interface java.util.Map<K,​V>
        Parameters:
        key - the mapping to remove
        Returns:
        the value mapped to the removed key, null if key not in map
        See Also:
        Map.remove(Object)
      • clear

        public void clear()
        Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues.
        Specified by:
        clear in interface java.util.Map<K,​V>
        Specified by:
        clear in interface Put<K,​V>
        See Also:
        Map.clear()
      • mapIterator

        public MapIterator<K,​V> mapIterator()
        Gets an iterator over the map. Changes made to the iterator affect this map.

        A MapIterator returns the keys in the map. It also provides convenient methods to get the key and value, and set the value. It avoids the need to create an entrySet/keySet/values object. It also avoids creating the Map Entry object.

        Specified by:
        mapIterator in interface IterableGet<K,​V>
        Returns:
        the map iterator
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Gets the entrySet view of the map. Changes made to the view affect this map.

        NOTE: from 4.0, the returned Map Entry will be an independent object and will not change anymore as the iterator progresses. To avoid this additional object creation and simply iterate through the entries, use mapIterator().

        Specified by:
        entrySet in interface Get<K,​V>
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Returns:
        the entrySet view
        See Also:
        Map.entrySet()
      • keySet

        public java.util.Set<K> keySet()
        Gets the keySet view of the map. Changes made to the view affect this map. To simply iterate through the keys, use mapIterator().
        Specified by:
        keySet in interface Get<K,​V>
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Returns:
        the keySet view
        See Also:
        Map.keySet()
      • values

        public java.util.Collection<V> values()
        Gets the values view of the map. Changes made to the view affect this map. To simply iterate through the values, use mapIterator().
        Specified by:
        values in interface Get<K,​V>
        Specified by:
        values in interface java.util.Map<K,​V>
        Returns:
        the values view
        See Also:
        Map.values()
      • clone

        public Flat3Map<K,​V> clone()
        Clones the map without cloning the keys or values.
        Returns:
        a shallow clone
        Since:
        3.1
      • equals

        public boolean equals​(java.lang.Object obj)
        Compares this map with another.
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the object to compare to
        Returns:
        true if equal
      • hashCode

        public int hashCode()
        Gets the standard Map hashCode.
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code defined in the Map interface
      • toString

        public java.lang.String toString()
        Gets the map as a String.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string version of the map