Class Long2ObjectHashMap<V>

  • Type Parameters:
    V - type of values stored in the Map
    All Implemented Interfaces:
    java.util.Map<java.lang.Long,​V>
    Direct Known Subclasses:
    Long2NullableObjectHashMap

    public class Long2ObjectHashMap<V>
    extends java.lang.Object
    implements java.util.Map<java.lang.Long,​V>
    Map implementation specialised for long keys using open addressing and linear probing for cache efficient access.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      int capacity()
      Get the total capacity for the map to which the load factor will be a fraction of.
      void clear()
      void compact()
      Compact the Map backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.
      private void compactChain​(int deleteIndex)  
      V compute​(long key, LongObjectToObjectFunction<? super V,​? extends V> remappingFunction)
      Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
      V compute​(java.lang.Long key, java.util.function.BiFunction<? super java.lang.Long,​? super V,​? extends V> remappingFunction)
      V computeIfAbsent​(long key, java.util.function.LongFunction<? extends V> mappingFunction)
      Get a value for a given key, or if it does not exist then default the value via a LongFunction and put it in the map.
      V computeIfAbsent​(java.lang.Long key, java.util.function.Function<? super java.lang.Long,​? extends V> mappingFunction)
      V computeIfPresent​(long key, LongObjectToObjectFunction<? super V,​? extends V> remappingFunction)
      If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
      V computeIfPresent​(java.lang.Long key, java.util.function.BiFunction<? super java.lang.Long,​? super V,​? extends V> remappingFunction)
      boolean containsKey​(long key)
      Overloaded version of Map.containsKey(Object) that takes a primitive long key.
      boolean containsKey​(java.lang.Object key)
      boolean containsValue​(java.lang.Object value)
      Long2ObjectHashMap.EntrySet entrySet()
      boolean equals​(java.lang.Object o)
      void forEach​(java.util.function.BiConsumer<? super java.lang.Long,​? super V> action)
      void forEachLong​(LongObjConsumer<V> consumer)
      Primitive specialised implementation of Map.forEach(BiConsumer).
      V get​(long key)
      Overloaded version of Map.get(Object) that takes a primitive long key.
      V get​(java.lang.Object key)
      protected V getMapped​(long key)
      Get mapped value without boxing the key.
      V getOrDefault​(long key, V defaultValue)
      Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
      int hashCode()
      private void increaseCapacity()  
      boolean isEmpty()
      Long2ObjectHashMap.KeySet keySet()
      float loadFactor()
      Get the load factor beyond which the map will increase size.
      void longForEach​(LongObjConsumer<V> consumer)
      Deprecated.
      protected java.lang.Object mapNullValue​(java.lang.Object value)
      Longerceptor for masking null values.
      V merge​(long key, V value, java.util.function.BiFunction<? super V,​? super V,​? extends V> remappingFunction)
      Primitive specialised version of Map.merge(Object, Object, BiFunction).
      V merge​(java.lang.Long key, V value, java.util.function.BiFunction<? super V,​? super V,​? extends V> remappingFunction)
      V put​(long key, V value)
      Overloaded version of Map.put(Object, Object) that takes a primitive long key.
      V put​(java.lang.Long key, V value)
      void putAll​(java.util.Map<? extends java.lang.Long,​? extends V> map)
      void putAll​(Long2ObjectHashMap<? extends V> map)
      Put all values from the given map longo this one without allocation.
      V putIfAbsent​(long key, V value)
      Primitive specialised version of Map.putIfAbsent(Object, Object).
      private void rehash​(int newCapacity)  
      V remove​(long key)
      Overloaded version of Map.remove(Object) that takes a primitive long key.
      boolean remove​(long key, V value)
      Primitive specialised version of Map.remove(Object, Object).
      V remove​(java.lang.Object key)
      boolean remove​(java.lang.Object key, java.lang.Object value)
      V replace​(long key, V value)
      Primitive specialised version of Map.replace(Object, Object).
      boolean replace​(long key, V oldValue, V newValue)
      Primitive specialised version of Map.replace(Object, Object, Object).
      void replaceAll​(java.util.function.BiFunction<? super java.lang.Long,​? super V,​? extends V> function)
      void replaceAllLong​(LongObjectToObjectFunction<? super V,​? extends V> function)
      Primitive specialised version of Map.replaceAll(BiFunction).
      int resizeThreshold()
      Get the actual threshold which when reached the map will resize.
      int size()
      java.lang.String toString()
      protected V unmapNullValue​(java.lang.Object value)
      Longerceptor for unmasking null values.
      Long2ObjectHashMap.ValueCollection values()
      • Methods inherited from class java.lang.Object

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

        getOrDefault, putIfAbsent, replace, replace
    • Constructor Detail

      • Long2ObjectHashMap

        public Long2ObjectHashMap​(int initialCapacity,
                                  float loadFactor)
        Constructs map with given initial capacity and load factory and enables caching of iterators.
        Parameters:
        initialCapacity - for the backing array.
        loadFactor - limit for resizing on puts.
      • Long2ObjectHashMap

        public Long2ObjectHashMap​(int initialCapacity,
                                  float loadFactor,
                                  boolean shouldAvoidAllocation)
        Construct a new map allowing a configuration for initial capacity and load factor.
        Parameters:
        initialCapacity - for the backing array.
        loadFactor - limit for resizing on puts.
        shouldAvoidAllocation - should allocation be avoided by caching iterators and map entries.
      • Long2ObjectHashMap

        public Long2ObjectHashMap​(Long2ObjectHashMap<V> mapToCopy)
        Copy construct a new map from an existing one.
        Parameters:
        mapToCopy - for construction.
    • Method Detail

      • loadFactor

        public float loadFactor()
        Get the load factor beyond which the map will increase size.
        Returns:
        load factor for when the map should increase size.
      • capacity

        public int capacity()
        Get the total capacity for the map to which the load factor will be a fraction of.
        Returns:
        the total capacity for the map.
      • resizeThreshold

        public int resizeThreshold()
        Get the actual threshold which when reached the map will resize. This is a function of the current capacity and load factor.
        Returns:
        the threshold when the map will resize.
      • size

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

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

        public void forEach​(java.util.function.BiConsumer<? super java.lang.Long,​? super V> action)
        Specified by:
        forEach in interface java.util.Map<java.lang.Long,​V>
      • forEachLong

        public void forEachLong​(LongObjConsumer<V> consumer)
        Primitive specialised implementation of Map.forEach(BiConsumer).

        NB: Renamed from forEach to avoid overloading on parameter types of lambda expression, which doesn't play well with type inference in lambda expressions.

        Parameters:
        consumer - a callback called for each key/value pair in the map.
      • containsKey

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

        public boolean containsKey​(long key)
        Overloaded version of Map.containsKey(Object) that takes a primitive long key.
        Parameters:
        key - for indexing the Map.
        Returns:
        true if the key is found otherwise false.
      • containsValue

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

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

        public V get​(long key)
        Overloaded version of Map.get(Object) that takes a primitive long key.
        Parameters:
        key - for indexing the Map.
        Returns:
        the value if found otherwise null.
      • getOrDefault

        public V getOrDefault​(long key,
                              V defaultValue)
        Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
        Parameters:
        key - whose associated value is to be returned.
        defaultValue - the default mapping of the key.
        Returns:
        the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
      • getMapped

        protected V getMapped​(long key)
        Get mapped value without boxing the key.
        Parameters:
        key - to get value by.
        Returns:
        mapped value or null.
      • computeIfAbsent

        public V computeIfAbsent​(java.lang.Long key,
                                 java.util.function.Function<? super java.lang.Long,​? extends V> mappingFunction)
        Specified by:
        computeIfAbsent in interface java.util.Map<java.lang.Long,​V>
      • computeIfAbsent

        public V computeIfAbsent​(long key,
                                 java.util.function.LongFunction<? extends V> mappingFunction)
        Get a value for a given key, or if it does not exist then default the value via a LongFunction and put it in the map.

        Primitive specialized version of Map.computeIfAbsent(Object, Function).

        Parameters:
        key - to search on.
        mappingFunction - to provide a value if the get returns null.
        Returns:
        the value if found otherwise the default.
      • computeIfPresent

        public V computeIfPresent​(java.lang.Long key,
                                  java.util.function.BiFunction<? super java.lang.Long,​? super V,​? extends V> remappingFunction)
        Specified by:
        computeIfPresent in interface java.util.Map<java.lang.Long,​V>
      • computeIfPresent

        public V computeIfPresent​(long key,
                                  LongObjectToObjectFunction<? super V,​? extends V> remappingFunction)
        If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.

        If the function returns null, the mapping is removed.

        Primitive specialized version of Map.computeIfPresent(Object, BiFunction).

        Parameters:
        key - to search on.
        remappingFunction - to provide a value if the get returns missingValue.
        Returns:
        the new value associated with the specified key, or null if none.
      • compute

        public V compute​(java.lang.Long key,
                         java.util.function.BiFunction<? super java.lang.Long,​? super V,​? extends V> remappingFunction)
        Specified by:
        compute in interface java.util.Map<java.lang.Long,​V>
      • compute

        public V compute​(long key,
                         LongObjectToObjectFunction<? super V,​? extends V> remappingFunction)
        Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).

        If the function returns null, the mapping is removed (or remains absent if initially absent).

        Primitive specialized version of Map.compute(Object, BiFunction).

        Parameters:
        key - to search on.
        remappingFunction - to provide a value if the get returns missingValue.
        Returns:
        the new value associated with the specified key, or null if none.
      • merge

        public V merge​(java.lang.Long key,
                       V value,
                       java.util.function.BiFunction<? super V,​? super V,​? extends V> remappingFunction)
        Specified by:
        merge in interface java.util.Map<java.lang.Long,​V>
      • merge

        public V merge​(long key,
                       V value,
                       java.util.function.BiFunction<? super V,​? super V,​? extends V> remappingFunction)
        Primitive specialised version of Map.merge(Object, Object, BiFunction).
        Parameters:
        key - with which the resulting value is to be associated.
        value - the non-null value to be merged with the existing value associated with the key or, if no existing value or a null value is associated with the key, to be associated with the key.
        remappingFunction - the function to recompute a value if present.
        Returns:
        the new value associated with the specified key, or null if no value is associated with the key.
      • put

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

        public V put​(long key,
                     V value)
        Overloaded version of Map.put(Object, Object) that takes a primitive long key.
        Parameters:
        key - for indexing the Map.
        value - to be inserted in the Map.
        Returns:
        the previous value if found otherwise null.
      • remove

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

        public V remove​(long key)
        Overloaded version of Map.remove(Object) that takes a primitive long key.
        Parameters:
        key - for indexing the Map.
        Returns:
        the value if found otherwise null.
      • remove

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

        public boolean remove​(long key,
                              V value)
        Primitive specialised version of Map.remove(Object, Object).
        Parameters:
        key - with which the specified value is associated.
        value - expected to be associated with the specified key.
        Returns:
        true if the value was removed.
      • clear

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

        public void compact()
        Compact the Map backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.
      • putAll

        public void putAll​(java.util.Map<? extends java.lang.Long,​? extends V> map)
        Specified by:
        putAll in interface java.util.Map<java.lang.Long,​V>
      • putAll

        public void putAll​(Long2ObjectHashMap<? extends V> map)
        Put all values from the given map longo this one without allocation.
        Parameters:
        map - whose value are to be added.
      • putIfAbsent

        public V putIfAbsent​(long key,
                             V value)
        Primitive specialised version of Map.putIfAbsent(Object, Object).
        Parameters:
        key - with which the specified value is to be associated.
        value - to be associated with the specified key.
        Returns:
        the previous value associated with the specified key, or null if there was no mapping for the key.
      • toString

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

        public boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Map<java.lang.Long,​V>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<java.lang.Long,​V>
        Overrides:
        hashCode in class java.lang.Object
      • mapNullValue

        protected java.lang.Object mapNullValue​(java.lang.Object value)
        Longerceptor for masking null values.
        Parameters:
        value - value to mask.
        Returns:
        masked value.
      • unmapNullValue

        protected V unmapNullValue​(java.lang.Object value)
        Longerceptor for unmasking null values.
        Parameters:
        value - value to unmask.
        Returns:
        unmasked value.
      • replace

        public V replace​(long key,
                         V value)
        Primitive specialised version of Map.replace(Object, Object).
        Parameters:
        key - key with which the specified value is associated.
        value - value to be associated with the specified key.
        Returns:
        the previous value associated with the specified key, or null if there was no mapping for the key.
      • replace

        public boolean replace​(long key,
                               V oldValue,
                               V newValue)
        Primitive specialised version of Map.replace(Object, Object, Object).
        Parameters:
        key - key with which the specified value is associated.
        oldValue - value expected to be associated with the specified key.
        newValue - value to be associated with the specified key.
        Returns:
        true if the value was replaced.
      • replaceAll

        public void replaceAll​(java.util.function.BiFunction<? super java.lang.Long,​? super V,​? extends V> function)
        Specified by:
        replaceAll in interface java.util.Map<java.lang.Long,​V>
      • replaceAllLong

        public void replaceAllLong​(LongObjectToObjectFunction<? super V,​? extends V> function)
        Primitive specialised version of Map.replaceAll(BiFunction).

        NB: Renamed from replaceAll to avoid overloading on parameter types of lambda expression, which doesn't play well with type inference in lambda expressions.

        Parameters:
        function - the function to apply to each entry.
      • increaseCapacity

        private void increaseCapacity()
      • rehash

        private void rehash​(int newCapacity)
      • compactChain

        private void compactChain​(int deleteIndex)