Class IdentityMap<K,​V>

  • All Implemented Interfaces:
    java.lang.Iterable<ObjectMap.Entry<K,​V>>

    public class IdentityMap<K,​V>
    extends ObjectMap<K,​V>
    An unordered map that uses identity comparison for the object keys. Null keys are not allowed. No allocation is done except when growing the table size.

    This class performs fast contains and remove (typically O(1), worst case O(n) but that is rare in practice). Add may be slightly slower, depending on hash collisions. Hashcodes are rehashed to reduce collisions and the need to resize. Load factors greater than 0.91 greatly increase the chances to resize to the next higher POT size.

    Unordered sets and maps are not designed to provide especially fast iteration.

    This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using Fibonacci hashing, instead of the more common power-of-two mask, to better distribute poor hashCodes (see Malte Skarupke's blog post). Linear probing continues to work even when all hashCodes collide, just more slowly.

    Author:
    Tommy Ettinger, Nathan Sweet
    • Constructor Detail

      • IdentityMap

        public IdentityMap()
        Creates a new map with an initial capacity of 51 and a load factor of 0.8.
      • IdentityMap

        public IdentityMap​(int initialCapacity)
        Creates a new map with a load factor of 0.8.
        Parameters:
        initialCapacity - If not a power of two, it is increased to the next nearest power of two.
      • IdentityMap

        public IdentityMap​(int initialCapacity,
                           float loadFactor)
        Creates a new map with the specified initial capacity and load factor. This map will hold initialCapacity items before growing the backing table.
        Parameters:
        initialCapacity - If not a power of two, it is increased to the next nearest power of two.
      • IdentityMap

        public IdentityMap​(IdentityMap<K,​V> map)
        Creates a new map identical to the specified map.
    • Method Detail

      • place

        protected int place​(K item)
        Description copied from class: ObjectMap
        Returns an index >= 0 and <= ObjectMap.mask for the specified item.

        The default implementation uses Fibonacci hashing on the item's Object.hashCode(): the hashcode is multiplied by a long constant (2 to the 64th, divided by the golden ratio) then the uppermost bits are shifted into the lowest positions to obtain an index in the desired range. Multiplication by a long may be slower than int (eg on GWT) but greatly improves rehashing, allowing even very poor hashcodes, such as those that only differ in their upper bits, to be used without high collision rates. Fibonacci hashing has increased collision rates when all or most hashcodes are multiples of larger Fibonacci numbers (see Malte Skarupke's blog post).

        This method can be overriden to customizing hashing. This may be useful eg in the unlikely event that most hashcodes are Fibonacci numbers, if keys provide poor or incorrect hashcodes, or to simplify hashing if keys provide high quality hashcodes and don't need Fibonacci hashing: return item.hashCode() & mask;

        Overrides:
        place in class ObjectMap<K,​V>