Class FieldBasedScalingMap<K,​V>

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

    public final class FieldBasedScalingMap<K,​V>
    extends Object
    implements Map<K,​V>
    Uses a given Map as storage unless it is the first and only entry, in which case it uses fields. This helps avoid the overhead of creating and accessing a hash map if we only have 1 entry. This implementation is not thread-safe, regardless of the underlying Map. Iteration order of elements is specified by the underlying Map.
    • Constructor Detail

      • FieldBasedScalingMap

        public FieldBasedScalingMap​(Supplier<Map<K,​V>> mapSupplier)
    • Method Detail

      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface Map<K,​V>
      • putAll

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

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

        public Set<K> keySet()
        As defined by Map.keySet(). May throw an exception if the iterator is used to remove an element while the map is based on a field.
        Specified by:
        keySet in interface Map<K,​V>
        Returns:
        never null
      • values

        public Collection<V> values()
        As defined by Map.values(). May throw an exception if the iterator is used to remove an element while the map is based on a field.
        Specified by:
        values in interface Map<K,​V>
        Returns:
        never null
      • entrySet

        public Set<Map.Entry<K,​V>> entrySet()
        As defined by Map.entrySet(). May throw an exception if the iterator is used to remove an element while the map is based on a field.
        Specified by:
        entrySet in interface Map<K,​V>
        Returns:
        never null
      • forEach

        public void forEach​(BiConsumer<? super K,​? super V> action)
        Specified by:
        forEach in interface Map<K,​V>
      • remove

        public V remove​(Object key)
        Specified by:
        remove in interface Map<K,​V>
      • get

        public V get​(Object key)
        Specified by:
        get in interface Map<K,​V>
      • size

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

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

        public boolean containsKey​(Object key)
        Specified by:
        containsKey in interface Map<K,​V>