Class Object2ObjectLinkedOpenHashMap<K,​V>

  • All Implemented Interfaces:
    Function<K,​V>, Hash, Object2ObjectFunction<K,​V>, Object2ObjectMap<K,​V>, Object2ObjectSortedMap<K,​V>, java.io.Serializable, java.lang.Cloneable, java.util.function.Function<K,​V>, java.util.Map<K,​V>, java.util.SortedMap<K,​V>

    public class Object2ObjectLinkedOpenHashMap<K,​V>
    extends AbstractObject2ObjectSortedMap<K,​V>
    implements java.io.Serializable, java.lang.Cloneable, Hash
    A type-specific linked hash map with with a fast, small-footprint implementation.

    Instances of this class use a hash table to represent a map. The table is filled up to a specified load factor, and then doubled in size to accommodate new entries. If the table is emptied below one fourth of the load factor, it is halved in size; however, the table is never reduced to a size smaller than that at creation time: this approach makes it possible to create maps with a large capacity in which insertions and deletions do not cause immediately rehashing. Moreover, halving is not performed when deleting entries from an iterator, as it would interfere with the iteration process.

    Note that clear() does not modify the hash table size. Rather, a family of trimming methods lets you control the size of the table; this is particularly useful if you reuse instances of this class.

    Entries returned by the type-specific Object2ObjectMap.entrySet() method implement the suitable type-specific Pair interface; only values are mutable.

    Iterators generated by this map will enumerate pairs in the same order in which they have been added to the map (addition of pairs whose key is already present in the map does not change the iteration order). Note that this order has nothing in common with the natural order of the keys. The order is kept by means of a doubly linked list, represented via an array of longs parallel to the table.

    This class implements the interface of a sorted map, so to allow easy access of the iteration order: for instance, you can get the first key in iteration order with firstKey() without having to create an iterator; however, this class partially violates the SortedMap contract because all submap methods throw an exception and comparator() returns always null.

    Additional methods, such as getAndMoveToFirst(), make it easy to use instances of this class as a cache (e.g., with LRU policy).

    The iterators provided by the views of this class using are type-specific list iterators, and can be started at any element which is a key of the map, or a NoSuchElementException exception will be thrown. If, however, the provided element is not the first or last key in the map, the first access to the list index will require linear time, as in the worst case the entire key set must be scanned in iteration order to retrieve the positional index of the starting key. If you use just the methods of a type-specific BidirectionalIterator, however, all operations will be performed in constant time.

    See Also:
    Hash, HashCommon, Serialized Form
    • Constructor Detail

      • Object2ObjectLinkedOpenHashMap

        public Object2ObjectLinkedOpenHashMap​(int expected,
                                              float f)
        Creates a new hash map.

        The actual table size will be the least power of two greater than expected/f.

        Parameters:
        expected - the expected number of elements in the hash map.
        f - the load factor.
      • Object2ObjectLinkedOpenHashMap

        public Object2ObjectLinkedOpenHashMap​(int expected)
        Creates a new hash map with Hash.DEFAULT_LOAD_FACTOR as load factor.
        Parameters:
        expected - the expected number of elements in the hash map.
      • Object2ObjectLinkedOpenHashMap

        public Object2ObjectLinkedOpenHashMap​(java.util.Map<? extends K,​? extends V> m,
                                              float f)
        Creates a new hash map copying a given one.
        Parameters:
        m - a Map to be copied into the new hash map.
        f - the load factor.
      • Object2ObjectLinkedOpenHashMap

        public Object2ObjectLinkedOpenHashMap​(java.util.Map<? extends K,​? extends V> m)
        Creates a new hash map with Hash.DEFAULT_LOAD_FACTOR as load factor copying a given one.
        Parameters:
        m - a Map to be copied into the new hash map.
      • Object2ObjectLinkedOpenHashMap

        public Object2ObjectLinkedOpenHashMap​(Object2ObjectMap<K,​V> m,
                                              float f)
        Creates a new hash map copying a given type-specific one.
        Parameters:
        m - a type-specific map to be copied into the new hash map.
        f - the load factor.
      • Object2ObjectLinkedOpenHashMap

        public Object2ObjectLinkedOpenHashMap​(Object2ObjectMap<K,​V> m)
        Creates a new hash map with Hash.DEFAULT_LOAD_FACTOR as load factor copying a given type-specific one.
        Parameters:
        m - a type-specific map to be copied into the new hash map.
      • Object2ObjectLinkedOpenHashMap

        public Object2ObjectLinkedOpenHashMap​(K[] k,
                                              V[] v,
                                              float f)
        Creates a new hash map using the elements of two parallel arrays.
        Parameters:
        k - the array of keys of the new hash map.
        v - the array of corresponding values in the new hash map.
        f - the load factor.
        Throws:
        java.lang.IllegalArgumentException - if k and v have different lengths.
      • Object2ObjectLinkedOpenHashMap

        public Object2ObjectLinkedOpenHashMap​(K[] k,
                                              V[] v)
        Creates a new hash map with Hash.DEFAULT_LOAD_FACTOR as load factor using the elements of two parallel arrays.
        Parameters:
        k - the array of keys of the new hash map.
        v - the array of corresponding values in the new hash map.
        Throws:
        java.lang.IllegalArgumentException - if k and v have different lengths.
    • Method Detail

      • ensureCapacity

        public void ensureCapacity​(int capacity)
        Ensures that this map can hold a certain number of keys without rehashing.
        Parameters:
        capacity - a number of keys; there will be no rehashing unless the map size exceeds this number.
      • removeFirst

        public V removeFirst()
        Removes the mapping associated with the first key in iteration order.
        Returns:
        the value previously associated with the first key in iteration order.
        Throws:
        java.util.NoSuchElementException - is this map is empty.
      • removeLast

        public V removeLast()
        Removes the mapping associated with the last key in iteration order.
        Returns:
        the value previously associated with the last key in iteration order.
        Throws:
        java.util.NoSuchElementException - is this map is empty.
      • getAndMoveToFirst

        public V getAndMoveToFirst​(K k)
        Returns the value to which the given key is mapped; if the key is present, it is moved to the first position of the iteration order.
        Parameters:
        k - the key.
        Returns:
        the corresponding value, or the default return value if no value was present for the given key.
      • getAndMoveToLast

        public V getAndMoveToLast​(K k)
        Returns the value to which the given key is mapped; if the key is present, it is moved to the last position of the iteration order.
        Parameters:
        k - the key.
        Returns:
        the corresponding value, or the default return value if no value was present for the given key.
      • putAndMoveToFirst

        public V putAndMoveToFirst​(K k,
                                   V v)
        Adds a pair to the map; if the key is already present, it is moved to the first position of the iteration order.
        Parameters:
        k - the key.
        v - the value.
        Returns:
        the old value, or the default return value if no value was present for the given key.
      • putAndMoveToLast

        public V putAndMoveToLast​(K k,
                                  V v)
        Adds a pair to the map; if the key is already present, it is moved to the last position of the iteration order.
        Parameters:
        k - the key.
        v - the value.
        Returns:
        the old value, or the default return value if no value was present for the given key.
      • containsKey

        public boolean containsKey​(java.lang.Object k)
        Description copied from class: AbstractObject2ObjectMap
        Returns true if this function contains a mapping for the specified key.
        Specified by:
        containsKey in interface Function<K,​V>
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Specified by:
        containsKey in interface Object2ObjectMap<K,​V>
        Overrides:
        containsKey in class AbstractObject2ObjectMap<K,​V>
        Parameters:
        k - the key.
        Returns:
        true if this function associates a value to key.
        See Also:
        Map.containsKey(Object)
      • getOrDefault

        public V getOrDefault​(java.lang.Object k,
                              V defaultValue)
        Returns the value to which the specified key is mapped, or the defaultValue if this map contains no mapping for the key.
        Specified by:
        getOrDefault in interface Function<K,​V>
        Specified by:
        getOrDefault in interface java.util.Map<K,​V>
        Specified by:
        getOrDefault in interface Object2ObjectFunction<K,​V>
        Specified by:
        getOrDefault in interface Object2ObjectMap<K,​V>
        Parameters:
        k - the key.
        defaultValue - the default mapping of the key.
        Returns:
        the value to which the specified key is mapped, or the defaultValue if this map contains no mapping for the key.
        See Also:
        Map.getOrDefault(Object, Object)
      • putIfAbsent

        public V putIfAbsent​(K k,
                             V v)
        If the specified key is not already associated with a value, associates it with the given value and returns the default return value, else returns the current value.
        Specified by:
        putIfAbsent in interface java.util.Map<K,​V>
        Specified by:
        putIfAbsent in interface Object2ObjectMap<K,​V>
        Parameters:
        k - key with which the specified value is to be associated.
        v - value to be associated with the specified key.
        Returns:
        the previous value associated with the specified key, or the default return value if there was no mapping for the key.
        See Also:
        Map.putIfAbsent(Object, Object)
      • remove

        public boolean remove​(java.lang.Object k,
                              java.lang.Object v)
        Removes the entry for the specified key only if it is currently mapped to the specified value.
        Specified by:
        remove in interface java.util.Map<K,​V>
        Specified by:
        remove in interface Object2ObjectMap<K,​V>
        Parameters:
        k - key with which the specified value is associated.
        v - value expected to be associated with the specified key.
        Returns:
        true if the value was removed.
        See Also:
        Map.remove(Object, Object)
      • replace

        public boolean replace​(K k,
                               V oldValue,
                               V v)
        Replaces the entry for the specified key only if currently mapped to the specified value.
        Specified by:
        replace in interface java.util.Map<K,​V>
        Specified by:
        replace in interface Object2ObjectMap<K,​V>
        Parameters:
        k - key with which the specified value is associated.
        oldValue - value expected to be associated with the specified key.
        v - value to be associated with the specified key.
        Returns:
        true if the value was replaced.
        See Also:
        Map.replace(Object, Object, Object)
      • replace

        public V replace​(K k,
                         V v)
        Replaces the entry for the specified key only if it is currently mapped to some value.
        Specified by:
        replace in interface java.util.Map<K,​V>
        Specified by:
        replace in interface Object2ObjectMap<K,​V>
        Parameters:
        k - key with which the specified value is associated.
        v - value to be associated with the specified key.
        Returns:
        the previous value associated with the specified key, or the default return value if there was no mapping for the key.
        See Also:
        Map.replace(Object, Object)
      • computeIfAbsent

        public V computeIfAbsent​(K key,
                                 Object2ObjectFunction<? super K,​? extends V> mappingFunction)
        If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map, unless the key is not present in the given mapping function.

        This version of computeIfAbsent() uses a type-specific version of fastutil's Function. Since Function has a containsKey() method, it is possible to avoid adding a key by having containsKey() return false for that key.

        Specified by:
        computeIfAbsent in interface Object2ObjectMap<K,​V>
        Parameters:
        key - key with which the specified value is to be associated.
        mappingFunction - the function to compute a value.
        Returns:
        the current (existing or computed) value associated with the specified key.
        See Also:
        Map.computeIfAbsent(Object, java.util.function.Function)
      • computeIfPresent

        public V computeIfPresent​(K k,
                                  java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)
        If the value for the specified key is present, attempts to compute a new mapping given the key and its current mapped value.
        Specified by:
        computeIfPresent in interface java.util.Map<K,​V>
        Specified by:
        computeIfPresent in interface Object2ObjectMap<K,​V>
        Parameters:
        k - key with which the specified value is to be associated.
        remappingFunction - the function to compute a value.
        Returns:
        the new value associated with the specified key, or the default return value if none.
        See Also:
        Map.computeIfPresent(Object, java.util.function.BiFunction)
      • compute

        public V compute​(K k,
                         java.util.function.BiFunction<? super K,​? 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). If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.

        Specified by:
        compute in interface java.util.Map<K,​V>
        Specified by:
        compute in interface Object2ObjectMap<K,​V>
        Parameters:
        k - key with which the specified value is to be associated.
        remappingFunction - the function to compute a value.
        Returns:
        the new value associated with the specified key, or the default return value if none.
        See Also:
        Map.compute(Object, java.util.function.BiFunction)
      • merge

        public V merge​(K k,
                       V v,
                       java.util.function.BiFunction<? super V,​? super V,​? extends V> remappingFunction)
        If the specified key is not already associated with a value, associates it with the given value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null.
        Specified by:
        merge in interface java.util.Map<K,​V>
        Specified by:
        merge in interface Object2ObjectMap<K,​V>
        Parameters:
        k - key with which the resulting value is to be associated.
        v - the value to be merged with the existing value associated with the key or, if no existing 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 the default return value if no value is associated with the key.
        See Also:
        Map.merge(Object, Object, java.util.function.BiFunction)
      • clear

        public void clear()
        Description copied from interface: Object2ObjectMap
        Removes all of the mappings from this map (optional operation). The map will be empty after this call returns.
        Specified by:
        clear in interface Function<K,​V>
        Specified by:
        clear in interface java.util.Map<K,​V>
        Specified by:
        clear in interface Object2ObjectMap<K,​V>
        See Also:
        Map.clear()
      • size

        public int size()
        Description copied from interface: Object2ObjectMap
        Returns the number of key/value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
        Specified by:
        size in interface Function<K,​V>
        Specified by:
        size in interface java.util.Map<K,​V>
        Specified by:
        size in interface Object2ObjectMap<K,​V>
        Returns:
        the number of key-value mappings in this map.
        See Also:
        Size64
      • firstKey

        public K firstKey()
        Returns the first key of this map in iteration order.
        Specified by:
        firstKey in interface java.util.SortedMap<K,​V>
        Returns:
        the first key in iteration order.
      • lastKey

        public K lastKey()
        Returns the last key of this map in iteration order.
        Specified by:
        lastKey in interface java.util.SortedMap<K,​V>
        Returns:
        the last key in iteration order.
      • tailMap

        public Object2ObjectSortedMap<K,​V> tailMap​(K from)
        Returns a view of the portion of this sorted map whose keys are greater than or equal to fromKey.
        Specified by:
        tailMap in interface Object2ObjectSortedMap<K,​V>
        Specified by:
        tailMap in interface java.util.SortedMap<K,​V>
        See Also:
        SortedMap.tailMap(Object)
        Implementation Specification:
        This implementation just throws an UnsupportedOperationException.
      • headMap

        public Object2ObjectSortedMap<K,​V> headMap​(K to)
        Returns a view of the portion of this sorted map whose keys are strictly less than toKey.
        Specified by:
        headMap in interface Object2ObjectSortedMap<K,​V>
        Specified by:
        headMap in interface java.util.SortedMap<K,​V>
        See Also:
        SortedMap.headMap(Object)
        Implementation Specification:
        This implementation just throws an UnsupportedOperationException.
      • subMap

        public Object2ObjectSortedMap<K,​V> subMap​(K from,
                                                        K to)
        Returns a view of the portion of this sorted map whose keys range from fromKey, inclusive, to toKey, exclusive.
        Specified by:
        subMap in interface Object2ObjectSortedMap<K,​V>
        Specified by:
        subMap in interface java.util.SortedMap<K,​V>
        See Also:
        SortedMap.subMap(Object,Object)
        Implementation Specification:
        This implementation just throws an UnsupportedOperationException.
      • comparator

        public java.util.Comparator<? super K> comparator()
        Returns the comparator associated with this sorted set, or null if it uses its keys' natural ordering.
        Specified by:
        comparator in interface Object2ObjectSortedMap<K,​V>
        Specified by:
        comparator in interface java.util.SortedMap<K,​V>
        See Also:
        SortedMap.comparator()
        Implementation Specification:
        This implementation just returns null.
      • keySet

        public ObjectSortedSet<K> keySet()
        Description copied from class: AbstractObject2ObjectSortedMap
        Returns a type-specific-set view of the keys of this map.

        The view is backed by the set returned by Map.entrySet(). Note that no attempt is made at caching the result of this method, as this would require adding some attributes that lightweight implementations would not need. Subclasses may easily override this policy by calling this method and caching the result, but implementors are encouraged to write more efficient ad-hoc implementations.

        The view is backed by the sorted set returned by Map.entrySet(). Note that no attempt is made at caching the result of this method, as this would require adding some attributes that lightweight implementations would not need. Subclasses may easily override this policy by calling this method and caching the result, but implementors are encouraged to write more efficient ad-hoc implementations.

        Specified by:
        keySet in interface java.util.Map<K,​V>
        Specified by:
        keySet in interface Object2ObjectMap<K,​V>
        Specified by:
        keySet in interface Object2ObjectSortedMap<K,​V>
        Specified by:
        keySet in interface java.util.SortedMap<K,​V>
        Overrides:
        keySet in class AbstractObject2ObjectSortedMap<K,​V>
        Returns:
        a sorted set view of the keys of this map; it may be safely cast to a type-specific interface.
        See Also:
        Map.keySet()
      • values

        public ObjectCollection<V> values()
        Description copied from class: AbstractObject2ObjectSortedMap
        Returns a type-specific-set view of the values of this map.

        The view is backed by the set returned by Map.entrySet(). Note that no attempt is made at caching the result of this method, as this would require adding some attributes that lightweight implementations would not need. Subclasses may easily override this policy by calling this method and caching the result, but implementors are encouraged to write more efficient ad-hoc implementations.

        The view is backed by the sorted set returned by Map.entrySet(). Note that no attempt is made at caching the result of this method, as this would require adding some attributes that lightweight implementations would not need. Subclasses may easily override this policy by calling this method and caching the result, but implementors are encouraged to write more efficient ad-hoc implementations.

        Specified by:
        values in interface java.util.Map<K,​V>
        Specified by:
        values in interface Object2ObjectMap<K,​V>
        Specified by:
        values in interface Object2ObjectSortedMap<K,​V>
        Specified by:
        values in interface java.util.SortedMap<K,​V>
        Overrides:
        values in class AbstractObject2ObjectSortedMap<K,​V>
        Returns:
        a type-specific collection view of the values contained in this map.
        See Also:
        Map.values()
      • trim

        public boolean trim()
        Rehashes the map, making the table as small as possible.

        This method rehashes the table to the smallest size satisfying the load factor. It can be used when the set will not be changed anymore, so to optimize access speed and size.

        If the table size is already the minimum possible, this method does nothing.

        Returns:
        true if there was enough memory to trim the map.
        See Also:
        trim(int)
      • trim

        public boolean trim​(int n)
        Rehashes this map if the table is too large.

        Let N be the smallest table size that can hold max(n,size()) entries, still satisfying the load factor. If the current table size is smaller than or equal to N, this method does nothing. Otherwise, it rehashes this map in a table of size N.

        This method is useful when reusing maps. Clearing a map leaves the table size untouched. If you are reusing a map many times, you can call this method with a typical size to avoid keeping around a very large table just because of a few large transient maps.

        Parameters:
        n - the threshold for the trimming.
        Returns:
        true if there was enough memory to trim the map.
        See Also:
        trim()
      • clone

        public Object2ObjectLinkedOpenHashMap<K,​V> clone()
        Returns a deep copy of this map.

        This method performs a deep copy of this hash map; the data stored in the map, however, is not cloned. Note that this makes a difference only for object keys.

        Returns:
        a deep copy of this map.
      • hashCode

        public int hashCode()
        Returns a hash code for this map. This method overrides the generic method provided by the superclass. Since equals() is not overriden, it is important that the value returned by this method is the same value as the one returned by the overriden method.
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class AbstractObject2ObjectMap<K,​V>
        Returns:
        a hash code for this map.