Class ArrayMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>

java.lang.Object
java.util.AbstractMap<K,V>
org.plumelib.util.ArrayMap<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
All Implemented Interfaces:
Cloneable, Map<K,V>

public class ArrayMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object> extends AbstractMap<K,V> implements Cloneable
A map backed by two arrays. It permits null keys and values, and its iterator has deterministic ordering.

Compared to a HashMap or LinkedHashMap: For very small maps, this uses much less space, has comparable performance, and (like a LinkedHashMap) is deterministic, with elements returned in the order their keys were inserted. For large maps, this is significantly less performant than other map implementations.

Compared to a TreeMap: This uses somewhat less space, and it does not require defining a comparator. This isn't sorted but does have deterministic ordering. For large maps, this is significantly less performant than other map implementations.

A number of other ArrayMap implementations exist, including

  • android.util.ArrayMap
  • com.google.api.client.util.ArrayMap
  • it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap
  • oracle.dss.util.ArrayMap
  • org.apache.myfaces.trinidad.util.ArrayMap
All of those use the Apache License, version 2.0, whereas this implementation is licensed under the more libral MIT License. In addition, some of those implementations forbid nulls or nondeterministically reorder the contents, and others don't specify their behavior regarding nulls and ordering.
  • Constructor Details

    • ArrayMap

      @SideEffectFree public ArrayMap(int initialCapacity)
      Constructs an empty ArrayMap with the specified initial capacity.
      Parameters:
      initialCapacity - the initial capacity
      Throws:
      IllegalArgumentException - if the initial capacity is negative
    • ArrayMap

      @SideEffectFree public ArrayMap()
      Constructs an empty ArrayMap with the default initial capacity.
    • ArrayMap

      @SideEffectFree public ArrayMap(Map<? extends K,? extends V> m)
      Constructs a new ArrayMap with the same mappings as the given Map.
      Parameters:
      m - the map whose mappings are to be placed in this map
      Throws:
      NullPointerException - if the given map is null
  • Method Details

    • newArrayMapOrHashMap

      public static <K, V> Map<K,V> newArrayMapOrHashMap(int capacity)
      Returns a new ArrayMap or HashMap with the given capacity. Uses an ArrayMap if the capacity is small, and a HashMap otherwise.
      Type Parameters:
      K - the type of the keys
      V - the type of the values
      Parameters:
      capacity - the expected maximum number of elements in the set
      Returns:
      a new ArrayMap or HashMap with the given capacity
    • newArrayMapOrHashMap

      public static <K, V> Map<K,V> newArrayMapOrHashMap(Map<K,V> m)
      Returns a new ArrayMap or HashMap with the given elements. Uses an ArrayMap if the capacity is small, and a HashMap otherwise.
      Type Parameters:
      K - the type of the keys
      V - the type of the values
      Parameters:
      m - the elements to put in the returned set
      Returns:
      a new ArrayMap or HashMap with the given elements
    • newArrayMapOrLinkedHashMap

      public static <K, V> Map<K,V> newArrayMapOrLinkedHashMap(int capacity)
      Returns a new ArrayMap or LinkedHashMap with the given capacity. Uses an ArrayMap if the capacity is small, and a LinkedHashMap otherwise.
      Type Parameters:
      K - the type of the keys
      V - the type of the values
      Parameters:
      capacity - the expected maximum number of elements in the set
      Returns:
      a new ArrayMap or LinkedHashMap with the given capacity
    • newArrayMapOrLinkedHashMap

      public static <K, V> Map<K,V> newArrayMapOrLinkedHashMap(Map<K,V> m)
      Returns a new ArrayMap or LinkedHashMap with the given elements. Uses an ArrayMap if the capacity is small, and a LinkedHashMap otherwise.
      Type Parameters:
      K - the type of the keys
      V - the type of the values
      Parameters:
      m - the elements to put in the returned set
      Returns:
      a new ArrayMap or LinkedHashMap with the given elements
    • size

      @Pure public @org.checkerframework.checker.index.qual.NonNegative int size()
      Specified by:
      size in interface Map<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
      Overrides:
      size in class AbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
    • isEmpty

      @Pure public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
      Overrides:
      isEmpty in class AbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
    • containsKey

      @Pure public boolean containsKey(@GuardSatisfied @Nullable @UnknownSignedness Object key)
      Specified by:
      containsKey in interface Map<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
      Overrides:
      containsKey in class AbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
    • containsValue

      @Pure public boolean containsValue(@GuardSatisfied @Nullable @UnknownSignedness Object value)
      Specified by:
      containsValue in interface Map<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
      Overrides:
      containsValue in class AbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
    • get

      @Pure public @Nullable V get(@GuardSatisfied @Nullable @UnknownSignedness Object key)
      Specified by:
      get in interface Map<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
      Overrides:
      get in class AbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
    • put

      public @Nullable V put(K key, V value)
      Specified by:
      put in interface Map<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
      Overrides:
      put in class AbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
    • remove

      public @Nullable V remove(@GuardSatisfied @Nullable @UnknownSignedness Object key)
      Specified by:
      remove in interface Map<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
      Overrides:
      remove in class AbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Specified by:
      putAll in interface Map<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
      Overrides:
      putAll in class AbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
      Overrides:
      clear in class AbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
    • keySet

      @Pure public Set<@KeyFor("this") K> keySet()
      Specified by:
      keySet in interface Map<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
      Overrides:
      keySet in class AbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
    • values

      @Pure public Collection<V> values()
      Specified by:
      values in interface Map<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
      Overrides:
      values in class AbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
    • entrySet

      @Pure public Set<Map.Entry<@KeyFor("this") K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
      Specified by:
      entrySet in class AbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
    • getOrDefault

      @SideEffectFree public V getOrDefault(@GuardSatisfied @Nullable @UnknownSignedness Object key, V defaultValue)
      Specified by:
      getOrDefault in interface Map<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
    • forEach

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

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

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

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

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

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

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

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

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

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

      @SideEffectFree public ArrayMap<K,V> clone()
      Returns a copy of this.
      Overrides:
      clone in class AbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
      Returns:
      a copy of this