Class ArrayMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
icyllis.modernui.util.ArrayMap<K,V>
All Implemented Interfaces:
Map<K,V>

public class ArrayMap<K,V> extends AbstractMap<K,V> implements Map<K,V>
ArrayMap is a generic key->value mapping data structure that is designed to be more memory efficient than a HashMap. It keeps its mappings in an array data structure -- an integer array of hash codes for each item, and an Object array of the key/value pairs. This allows it to avoid having to create an extra object for every entry put in to the map, and it also tries to control the growth of the size of these arrays more aggressively (since growing them only requires copying the entries in the array, not rebuilding a hash map).

Note that this implementation is not intended to be appropriate for data structures that may contain large numbers of items. It is generally slower than a traditional HashMap, since lookups require a binary search and adds and removes require inserting and deleting entries in the array. For containers holding up to hundreds of items, the performance difference is not significant, less than 50%.

Because this container is intended to better balance memory use, unlike most other standard Java containers it will shrink its array as items are removed from it. Currently you have no control over this shrinking -- if you set a capacity and then remove an item, it may reduce the capacity to better match the current size. In the future an explicit call to set the capacity should turn off this aggressive shrinking behavior.

This structure is NOT thread-safe.

See Also:
  • Object2ObjectOpenHashMap
  • Constructor Details

    • ArrayMap

      public ArrayMap()
      Create a new empty ArrayMap. The default capacity of an array map is 0, and will grow once items are added to it.
    • ArrayMap

      public ArrayMap(int initialCapacity)
      Create a new ArrayMap with a given initial capacity.
    • ArrayMap

      @Internal public ArrayMap(int initialCapacity, boolean identityHashCode)
    • ArrayMap

      public ArrayMap(@Nonnull Map<K,V> map)
      Create a new ArrayMap with the mappings from the given Map.
  • Method Details

    • clear

      public void clear()
      Make the array map empty. All storage is released.
      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class AbstractMap<K,V>
    • erase

      @Internal public void erase()
      Like clear(), but doesn't reduce the capacity of the ArrayMap.
    • ensureCapacity

      public void ensureCapacity(int minimumCapacity)
      Ensure the array map can hold at least minimumCapacity items.
    • containsKey

      public boolean containsKey(Object key)
      Check whether a key exists in the array.
      Specified by:
      containsKey in interface Map<K,V>
      Overrides:
      containsKey in class AbstractMap<K,V>
      Parameters:
      key - The key to search for.
      Returns:
      Returns true if the key exists, else false.
    • indexOfKey

      public int indexOfKey(Object key)
      Returns the index of a key in the set.
      Parameters:
      key - The key to search for.
      Returns:
      Returns the index of the key if it exists, else a negative integer.
    • indexOfValue

      public int indexOfValue(Object value)
      Returns an index for which valueAt(int) would return the specified value, or a negative number if no keys map to the specified value. Beware that this is a linear search, unlike lookups by key, and that multiple keys can map to the same value and this will find only one of them.
    • containsValue

      public boolean containsValue(Object value)
      Check whether a value exists in the array. This requires a linear search through the entire array.
      Specified by:
      containsValue in interface Map<K,V>
      Overrides:
      containsValue in class AbstractMap<K,V>
      Parameters:
      value - The value to search for.
      Returns:
      Returns true if the value exists, else false.
    • get

      public V get(Object key)
      Retrieve a value from the array.
      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class AbstractMap<K,V>
      Parameters:
      key - The key of the value to retrieve.
      Returns:
      Returns the value associated with the given key, or null if there is no such key.
    • keyAt

      public K keyAt(int index)
      Return the key at the given index in the array.

      For indices outside of the range 0...size()-1, an ArrayIndexOutOfBoundsException is thrown.

      Parameters:
      index - The desired index, must be between 0 and size()-1.
      Returns:
      Returns the key stored at the given index.
    • valueAt

      public V valueAt(int index)
      Return the value at the given index in the array.

      For indices outside of the range 0...size()-1, an ArrayIndexOutOfBoundsException is thrown.

      Parameters:
      index - The desired index, must be between 0 and size()-1.
      Returns:
      Returns the value stored at the given index.
    • setValueAt

      public V setValueAt(int index, V value)
      Set the value at a given index in the array.

      For indices outside of the range 0...size()-1, an ArrayIndexOutOfBoundsException is thrown.

      Parameters:
      index - The desired index, must be between 0 and size()-1.
      value - The new value to store at this index.
      Returns:
      Returns the previous value at the given index.
    • isEmpty

      public boolean isEmpty()
      Return true if the array map contains no items.
      Specified by:
      isEmpty in interface Map<K,V>
      Overrides:
      isEmpty in class AbstractMap<K,V>
    • put

      @Nullable public V put(K key, V value)
      Add a new value to the array map.
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class AbstractMap<K,V>
      Parameters:
      key - The key under which to store the value. If this key already exists in the array, its value will be replaced.
      value - The value to store for the given key.
      Returns:
      Returns the old value that was stored for the given key, or null if there was no such key.
    • append

      @Internal public void append(K key, V value)
      Special fast path for appending items to the end of the array without validation. The array must already be large enough to contain the item.
    • validate

      @Internal public void validate()
      The use of the append(Object, Object) function can result in invalid array maps, in particular an array map where the same key appears multiple times. This function verifies that the array map is valid, throwing IllegalArgumentException if a problem is found. The main use for this method is validating an array map after unpacking from an IPC, to protect against malicious callers.
    • forEach

      public void forEach(BiConsumer<? super K,? super V> action)
      Performs the given action for all elements in the stored order. This implementation overrides the default implementation to avoid iterating using the entrySet() and iterates in the key-value order consistent with keyAt(int) and valueAt(int).
      Specified by:
      forEach in interface Map<K,V>
      Parameters:
      action - The action to be performed for each element
    • putAll

      public void putAll(@Nonnull Map<? extends K,? extends V> map)
      Perform a put(Object, Object) of all key/value pairs in map
      Specified by:
      putAll in interface Map<K,V>
      Overrides:
      putAll in class AbstractMap<K,V>
      Parameters:
      map - The map whose contents are to be retrieved.
    • remove

      public V remove(Object key)
      Remove an existing key from the array map.
      Specified by:
      remove in interface Map<K,V>
      Overrides:
      remove in class AbstractMap<K,V>
      Parameters:
      key - The key of the mapping to remove.
      Returns:
      Returns the value that was stored under the key, or null if there was no such key.
    • removeAt

      public V removeAt(int index)
      Remove the key/value mapping at the given index.

      For indices outside of the range 0...size()-1 an ArrayIndexOutOfBoundsException is thrown.

      Parameters:
      index - The desired index, must be between 0 and size()-1.
      Returns:
      Returns the value that was stored at this index.
    • size

      public int size()
      Return the number of items in this array map.
      Specified by:
      size in interface Map<K,V>
      Overrides:
      size in class AbstractMap<K,V>
    • equals

      public boolean equals(@Nullable Object o)

      This implementation returns false if the object is not a map, or if the maps have different sizes. Otherwise, for each key in this map, values of both maps are compared. If the values for any key are not equal, the method returns false, otherwise it returns true.

      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class AbstractMap<K,V>
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class AbstractMap<K,V>
    • toString

      @Nonnull public String toString()

      This implementation composes a string by iterating over its mappings. If this map contains itself as a key or a value, the string "(this Map)" will appear in its place.

      Overrides:
      toString in class AbstractMap<K,V>
    • containsAll

      public boolean containsAll(@Nonnull Collection<?> collection)
      Determine if the array map contains all the keys in the given collection.
      Parameters:
      collection - The collection whose contents are to be checked against.
      Returns:
      Returns true if this array map contains a key for every entry in collection, else returns false.
    • removeAll

      public boolean removeAll(@Nonnull Collection<?> collection)
      Remove all keys in the array map that exist in the given collection.
      Parameters:
      collection - The collection whose contents are to be used to remove keys.
      Returns:
      Returns true if any keys were removed from the array map, else false.
    • retainAll

      public boolean retainAll(@Nonnull Collection<?> collection)
      Remove all keys in the array map that do not exist in the given collection.
      Parameters:
      collection - The collection whose contents are to be used to determine which keys to keep.
      Returns:
      Returns true if any keys were removed from the array map, else false.
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Return a Set for iterating over and interacting with all mappings in the array map.

      Note: this is a very inefficient way to access the array contents, it requires generating a number of temporary objects and allocates additional state information associated with the container that will remain for the life of the container.

      Note:

      the semantics of this Set are subtly different from that of a HashMap: most important, the Map.Entry object returned by its iterator is a single object that exists for the entire iterator, so you can not hold on to it after calling Iterator.next.

      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in class AbstractMap<K,V>
    • keySet

      public Set<K> keySet()
      Return a Set for iterating over and interacting with all keys in the array map.

      Note: this is a fairly inefficient way to access the array contents, it requires generating a number of temporary objects and allocates additional state information associated with the container that will remain for the life of the container.

      Specified by:
      keySet in interface Map<K,V>
      Overrides:
      keySet in class AbstractMap<K,V>
    • values

      public Collection<V> values()
      Return a Collection for iterating over and interacting with all values in the array map.

      Note: this is a fairly inefficient way to access the array contents, it requires generating a number of temporary objects and allocates additional state information associated with the container that will remain for the life of the container.

      Specified by:
      values in interface Map<K,V>
      Overrides:
      values in class AbstractMap<K,V>