Class UnsafeHashMap<K,V>

java.lang.Object
org.kingdoms.utils.internal.UnsafeHashMap<K,V>
All Implemented Interfaces:
Cloneable, Map<K,V>

public class UnsafeHashMap<K,V> extends Object implements Map<K,V>, Cloneable
  • Field Details

  • Constructor Details

    • UnsafeHashMap

      public UnsafeHashMap(int initialCapacity, float loadFactor)
    • UnsafeHashMap

      public UnsafeHashMap(int initialCapacity)
      Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).
      Parameters:
      initialCapacity - the initial capacity.
      Throws:
      IllegalArgumentException - if the initial capacity is negative.
    • UnsafeHashMap

      public UnsafeHashMap()
      Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
    • UnsafeHashMap

      public UnsafeHashMap(Map<? extends K,? extends V> m)
      Constructs a new HashMap with the same mappings as the specified Map. The HashMap is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified Map.
      Parameters:
      m - the map whose mappings are to be placed in this map
      Throws:
      NullPointerException - if the specified map is null
  • Method Details

    • of

      @SafeVarargs public static <K, V> UnsafeHashMap<K,V> of(Map.Entry<? extends K,? extends V>... entries)
    • hash

      public static int hash(Object key)
    • comparableClassFor

      public static Class<?> comparableClassFor(Object x)
    • compareComparables

      public static int compareComparables(Class<?> kc, Object k, Object x)
    • tableSizeFor

      public static int tableSizeFor(int cap)
    • putMapEntries

      public final void putMapEntries(Map<? extends K,? extends V> m, boolean evict)
    • putMapEntries

      public final void putMapEntries(Map<? extends K,? extends V> m, boolean ifAbsent, boolean evict)
      Implements Map.putAll and Map constructor
      Parameters:
      m - the map
      evict - false when initially constructing this map, else true (relayed to method afterNodeInsertion).
    • putEntries

      @SafeVarargs public final void putEntries(boolean evict, Map.Entry<? extends K,? extends V>... entries)
    • size

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

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

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

      public @Nullable UnsafeHashMap.Node<K,V> getNode(int hash, @NonNull Object key)
    • containsKey

      public boolean containsKey(@NonNull Object key)
      Specified by:
      containsKey in interface Map<K,V>
    • put

      public @Nullable V put(@NonNull K key, @Nullable V value)
      Specified by:
      put in interface Map<K,V>
    • putVal

      public final V putVal(int hash, @NonNull K key, @Nullable V value, boolean onlyIfAbsent, boolean evict)
    • resize

      public UnsafeHashMap.Node<K,V>[] resize()
    • treeifyBin

      public void treeifyBin(UnsafeHashMap.Node<K,V>[] tab, int hash)
    • putAll

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

      public void putAllIfAbsent(@NonNull Map<? extends K,? extends V> m)
    • remove

      public V remove(@NonNull Object key)
      Specified by:
      remove in interface Map<K,V>
    • removeNode

      public final @Nullable UnsafeHashMap.Node<K,V> removeNode(int hash, @NonNull Object key, @Nullable Object value, boolean matchValue, boolean movable)
      Implements Map.remove and related methods
      Parameters:
      hash - hash for key
      key - the key
      value - the value to match if matchValue, else ignored
      matchValue - if true only remove if value is equal
      movable - if false do not move other nodes while removing
      Returns:
      the node, or null if none
    • clear

      public void clear()
      Removes all of the mappings from this map. The map will be empty after this call returns.
      Specified by:
      clear in interface Map<K,V>
    • containsValue

      public boolean containsValue(@NonNull Object value)
      Returns true if this map maps one or more keys to the specified value.
      Specified by:
      containsValue in interface Map<K,V>
      Parameters:
      value - value whose presence in this map is to be tested
      Returns:
      true if this map maps one or more keys to the specified value
    • keySet

      public @NonNull Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
    • values

      public @NonNull Collection<V> values()
      Specified by:
      values in interface Map<K,V>
    • initValues

      public void initValues()
    • entrySet

      public @NonNull Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
    • getOrDefault

      public @Nullable V getOrDefault(@NonNull Object key, @Nullable V defaultValue)
      Specified by:
      getOrDefault in interface Map<K,V>
    • putIfAbsent

      public @Nullable V putIfAbsent(@NonNull K key, @Nullable V value)
      Specified by:
      putIfAbsent in interface Map<K,V>
    • remove

      public boolean remove(@NonNull Object key, @Nullable Object value)
      Specified by:
      remove in interface Map<K,V>
    • replace

      public boolean replace(@NonNull K key, V oldValue, V newValue)
      Specified by:
      replace in interface Map<K,V>
    • replace

      public @Nullable V replace(@NonNull K key, @Nullable V value)
      Specified by:
      replace in interface Map<K,V>
    • computeIfAbsent

      public @Nullable V computeIfAbsent(@NonNull K key, @NonNull Function<? super K,? extends V> mappingFunction)
      Specified by:
      computeIfAbsent in interface Map<K,V>
    • computeIfPresent

      public V computeIfPresent(@NonNull K key, @NonNull BiFunction<? super K,? super V,? extends V> remappingFunction)
      Specified by:
      computeIfPresent in interface Map<K,V>
    • compute

      public V compute(@NonNull K key, @NonNull BiFunction<? super K,? super V,? extends V> remappingFunction)
      Specified by:
      compute in interface Map<K,V>
    • merge

      public V merge(@NonNull K key, @Nullable V value, @NonNull BiFunction<? super V,? super V,? extends V> remappingFunction)
      Specified by:
      merge in interface Map<K,V>
    • forEach

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

      public void replaceAll(BiFunction<? super K,? super V,? extends V> function)
      Specified by:
      replaceAll in interface Map<K,V>
    • clone

      public Object clone()
      Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
      Overrides:
      clone in class Object
      Returns:
      a shallow copy of this map
    • capacity

      public final int capacity()
    • newNode

      public UnsafeHashMap.Node<K,V> newNode(int hash, K key, V value, UnsafeHashMap.Node<K,V> next)
    • replacementNode

      public UnsafeHashMap.Node<K,V> replacementNode(UnsafeHashMap.Node<K,V> p, UnsafeHashMap.Node<K,V> next)
    • newTreeNode

      public UnsafeHashMap.TreeNode<K,V> newTreeNode(int hash, K key, V value, UnsafeHashMap.Node<K,V> next)
    • replacementTreeNode

      public UnsafeHashMap.TreeNode<K,V> replacementTreeNode(UnsafeHashMap.Node<K,V> p, UnsafeHashMap.Node<K,V> next)
    • reinitialize

      public void reinitialize()
      Reset to initial default state. Called by clone and readObject.