Class LightHashMap<S,T>

java.lang.Object
java.util.AbstractMap<S,T>
com.biglybt.core.util.LightHashMap<S,T>
All Implemented Interfaces:
Cloneable, Map<S,T>
Direct Known Subclasses:
JSONObject, LightHashMapEx

public class LightHashMap<S,T> extends AbstractMap<S,T> implements Cloneable
A lighter (on memory) hash map
Advantages over HashMap:
  • Lower memory footprint
  • Everything is stored in a single array, this might improve cache performance (not verified)
  • Read-only operations on Key and Value iterators should be concurrency-safe (Entry iterators are not) but they might return null values unexpectedly under concurrent modification (not verified)
Disadvantages:
  • removal is implemented with thombstone-keys, this can significantly increase the lookup time if many values are removed. Use compactify() for scrubbing
  • entry set iterators and thus transfers to other maps are slower than comparable implementations
  • the map does not store hashcodes and relies on either the key-objects themselves caching them (such as strings) or a fast computation of hashcodes
  • concurrent modification detection is not as fail-fast as HashMap as no modification counter is used and only structural differences are noted
  • Field Details

    • THOMBSTONE

      private static final Object THOMBSTONE
    • NULLKEY

      private static final Object NULLKEY
    • DEFAULT_LOAD_FACTOR

      private static final float DEFAULT_LOAD_FACTOR
      See Also:
    • DEFAULT_CAPACITY

      private static final int DEFAULT_CAPACITY
      See Also:
    • loadFactor

      final float loadFactor
    • size

      int size
    • data

      Object[] data
  • Constructor Details

    • LightHashMap

      public LightHashMap()
    • LightHashMap

      public LightHashMap(int initialCapacity)
    • LightHashMap

      public LightHashMap(Map m)
    • LightHashMap

      public LightHashMap(int initialCapacity, float loadFactor)
  • Method Details

    • clone

      public Object clone()
      Overrides:
      clone in class AbstractMap<S,T>
    • entrySet

      public Set entrySet()
      Specified by:
      entrySet in interface Map<S,T>
      Specified by:
      entrySet in class AbstractMap<S,T>
    • put

      public T put(Object key, Object value)
      Specified by:
      put in interface Map<S,T>
      Overrides:
      put in class AbstractMap<S,T>
    • putAll

      public void putAll(Map m)
      Specified by:
      putAll in interface Map<S,T>
      Overrides:
      putAll in class AbstractMap<S,T>
    • keySet

      public Set<S> keySet()
      Specified by:
      keySet in interface Map<S,T>
      Overrides:
      keySet in class AbstractMap<S,T>
    • values

      public Collection<T> values()
      Specified by:
      values in interface Map<S,T>
      Overrides:
      values in class AbstractMap<S,T>
    • capacity

      public int capacity()
    • get

      public T get(Object key)
      Specified by:
      get in interface Map<S,T>
      Overrides:
      get in class AbstractMap<S,T>
    • add

      private Object add(Object key, Object value, boolean bulkAdd)
    • remove

      public T remove(Object key)
      Specified by:
      remove in interface Map<S,T>
      Overrides:
      remove in class AbstractMap<S,T>
    • removeForIndex

      private Object removeForIndex(int idx)
    • clear

      public void clear()
      Specified by:
      clear in interface Map<S,T>
      Overrides:
      clear in class AbstractMap<S,T>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<S,T>
      Overrides:
      containsKey in class AbstractMap<S,T>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<S,T>
      Overrides:
      containsValue in class AbstractMap<S,T>
    • keysEqual

      private boolean keysEqual(Object o1, Object o2)
    • findIndex

      private int findIndex(Object keyToFind)
    • nonModifyingFindIndex

      private int nonModifyingFindIndex(Object keyToFind)
    • checkCapacity

      private void checkCapacity(int n)
    • compactify

      public void compactify(float compactingLoadFactor)
      will shrink the internal storage size to the least possible amount, should be used after removing many entries for example
      Parameters:
      compactingLoadFactor - load factor for the compacting operation. Use 0f to compact with the load factor specified during instantiation. Use negative values of the desired load factors to compact only when it would reduce the storage size.
    • adjustCapacity

      private void adjustCapacity(int newSize)
    • test

      static void test()
    • main

      public static void main(String[] args)