Class ObjectIntMap<K>

java.lang.Object
com.github.tommyettinger.ds.ObjectIntMap<K>
All Implemented Interfaces:
Iterable<ObjectIntMap.Entry<K>>
Direct Known Subclasses:
ObjectIntOrderedMap

public class ObjectIntMap<K> extends Object implements Iterable<ObjectIntMap.Entry<K>>
An unordered map where the keys are objects and the values are unboxed ints. 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. Iteration is faster with Ordered types like ObjectOrderedSet and ObjectObjectOrderedMap.

You can customize most behavior of this map by extending it. place(Object) can be overridden to change how hashCodes are calculated (which can be useful for types like StringBuilder that don't implement hashCode()), and equate(Object, Object) can be overridden to change how equality is calculated.

This implementation uses linear probing with the backward shift algorithm for removal. It tries different hashes from a simple family, with the hash changing on resize. Linear probing continues to work even when all hashCodes collide; it just works more slowly in that case.

  • Field Details

    • size

      protected int size
    • keyTable

      protected K[] keyTable
    • valueTable

      protected int[] valueTable
    • loadFactor

      protected float loadFactor
      Between 0f (exclusive) and 1f (inclusive, if you're careful), this determines how full the backing tables can get before this increases their size. Larger values use less memory but make the data structure slower.
    • threshold

      protected int threshold
      Precalculated value of (int)(keyTable.length * loadFactor), used to determine when to resize.
    • shift

      protected int shift
      Used by place(Object) typically, this should always equal com.github.tommyettinger.digital.BitConversion.countLeadingZeros(mask). For a table that could hold 2 items (with 1 bit indices), this would be 64 - 1 == 63. For a table that could hold 256 items (with 8 bit indices), this would be 64 - 8 == 56.
    • mask

      protected int mask
      A bitmask used to confine hashcodes to the size of the table. Must be all 1 bits in its low positions, ie a power of two minus 1. If place(Object) is overridden, this can be used instead of shift to isolate usable bits of a hash.
    • hashMultiplier

      protected int hashMultiplier
      Used by place(Object) to mix hashCode() results. Changes on every call to resize(int) by default. This should always change when shift changes, meaning, when the backing table resizes. This only needs to be serialized if the full key and value tables are serialized, or if the iteration order should be the same before and after serialization. Iteration order is better handled by using ObjectIntOrderedMap.
    • entries1

      protected transient ObjectIntMap.Entries<K> entries1
    • entries2

      protected transient ObjectIntMap.Entries<K> entries2
    • values1

      protected transient ObjectIntMap.Values<K> values1
    • values2

      protected transient ObjectIntMap.Values<K> values2
    • keys1

      protected transient ObjectIntMap.Keys<K> keys1
    • keys2

      protected transient ObjectIntMap.Keys<K> keys2
    • defaultValue

      public int defaultValue
  • Constructor Details

    • ObjectIntMap

      public ObjectIntMap()
      Creates a new map with an initial capacity of Utilities.getDefaultTableCapacity() and a load factor of Utilities.getDefaultLoadFactor().
    • ObjectIntMap

      public ObjectIntMap(int initialCapacity)
      Creates a new map with the given starting capacity and a load factor of Utilities.getDefaultLoadFactor().
      Parameters:
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
    • ObjectIntMap

      public ObjectIntMap(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.
      loadFactor - what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1
    • ObjectIntMap

      public ObjectIntMap(ObjectIntMap<? extends K> map)
      Creates a new map identical to the specified map.
      Parameters:
      map - the map to copy
    • ObjectIntMap

      public ObjectIntMap(K[] keys, int[] values)
      Given two side-by-side arrays, one of keys, one of values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller array.
      Parameters:
      keys - an array of keys
      values - an array of values
    • ObjectIntMap

      public ObjectIntMap(Collection<? extends K> keys, PrimitiveCollection.OfInt values)
      Given two side-by-side collections, one of keys, one of values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller collection.
      Parameters:
      keys - a Collection of keys
      values - a PrimitiveCollection of values
  • Method Details

    • putAll

      public void putAll(Collection<? extends K> keys, PrimitiveCollection.OfInt values)
      Given two side-by-side collections, one of keys, one of values, this inserts each pair of key and value into this map with put().
      Parameters:
      keys - a Collection of keys
      values - a PrimitiveCollection of values
    • place

      protected int place(Object item)
      Returns an index >= 0 and <= mask for the specified item, mixed.
      Parameters:
      item - a non-null Object; its hashCode() method should be used by most implementations
      Returns:
      an index between 0 and mask (both inclusive)
    • equate

      protected boolean equate(Object left, Object right)
      Compares the objects left and right, which are usually keys, for equality, returning true if they are considered equal. This is used by the rest of this class to determine whether two keys are considered equal. Normally, this returns left.equals(right), but subclasses can override it to use reference equality, fuzzy equality, deep array equality, or any other custom definition of equality. Usually, place(Object) is also overridden if this method is.
      Parameters:
      left - must be non-null; typically a key being compared, but not necessarily
      right - may be null; typically a key being compared, but can often be null for an empty key slot, or some other type
      Returns:
      true if left and right are considered equal for the purposes of this class
    • locateKey

      protected int locateKey(Object key)
      Returns the index of the key if already present, else ~index for the next empty index. This calls equate(Object, Object) to determine if two keys are equivalent.
      Parameters:
      key - a non-null K key
      Returns:
      a negative index if the key was not found, or the non-negative index of the existing key if found
    • put

      public int put(K key, int value)
      Returns the old value associated with the specified key, or this map's defaultValue if there was no prior value.
    • putOrDefault

      public int putOrDefault(K key, int value, int defaultValue)
      Returns the old value associated with the specified key, or the given defaultValue if there was no prior value.
    • putAll

      public void putAll(ObjectIntMap<? extends K> map)
      Puts every key-value pair in the given map into this, with the values from the given map overwriting the previous values if two keys are identical.
      Parameters:
      map - a map with compatible key and value types; will not be modified
    • putAll

      public void putAll(K[] keys, int[] values)
      Given two side-by-side arrays, one of keys, one of values, this inserts each pair of key and value into this map with put().
      Parameters:
      keys - an array of keys
      values - an array of values
    • putAll

      public void putAll(K[] keys, int[] values, int length)
      Given two side-by-side arrays, one of keys, one of values, this inserts each pair of key and value into this map with put().
      Parameters:
      keys - an array of keys
      values - an array of values
      length - how many items from keys and values to insert, at-most
    • putAll

      public void putAll(K[] keys, int keyOffset, int[] values, int valueOffset, int length)
      Given two side-by-side arrays, one of keys, one of values, this inserts each pair of key and value into this map with put().
      Parameters:
      keys - an array of keys
      keyOffset - the first index in keys to insert
      values - an array of values
      valueOffset - the first index in values to insert
      length - how many items from keys and values to insert, at-most
    • putResize

      protected void putResize(K key, int value)
      Skips checks for existing keys, doesn't increment size.
    • get

      public int get(Object key)
      Returns the value for the specified key, or defaultValue if the key is not in the map.
      Parameters:
      key - a non-null Object that should almost always be a K (or an instance of a subclass of K)
    • getOrDefault

      public int getOrDefault(Object key, int defaultValue)
      Returns the value for the specified key, or the default value if the key is not in the map.
    • getAndIncrement

      public int getAndIncrement(K key, int defaultValue, int increment)
      Returns the key's current value and increments the stored value. If the key is not in the map, defaultValue + increment is put into the map and defaultValue is returned.
    • remove

      public int remove(Object key)
    • notEmpty

      public boolean notEmpty()
      Returns true if the map has one or more items.
    • size

      public int size()
      Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
      Returns:
      the number of key-value mappings in this map
    • isEmpty

      public boolean isEmpty()
      Returns true if the map is empty.
    • getDefaultValue

      public int getDefaultValue()
      Gets the default value, a int which is returned by get(Object) if the key is not found. If not changed, the default value is 0.
      Returns:
      the current default value
    • setDefaultValue

      public void setDefaultValue(int defaultValue)
      Sets the default value, a int which is returned by get(Object) if the key is not found. If not changed, the default value is 0. Note that getOrDefault(Object, int) is also available, which allows specifying a "not-found" value per-call.
      Parameters:
      defaultValue - may be any int; should usually be one that doesn't occur as a typical value
    • shrink

      public void shrink(int maximumCapacity)
      Reduces the size of the backing arrays to be the specified capacity / loadFactor, or less. If the capacity is already less, nothing is done. If the map contains more items than the specified capacity, the next highest power of two capacity is used instead.
    • clear

      public void clear(int maximumCapacity)
      Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.
    • clear

      public void clear()
    • containsValue

      public boolean containsValue(int value)
      Returns true if the specified value is in the map. Note this traverses the entire map and compares every value, which may be an expensive operation.
    • containsKey

      public boolean containsKey(Object key)
    • findKey

      public K findKey(int value)
      Returns a key that maps to the specified value, or null if value is not in the map. Note, this traverses the entire map and compares every value, which may be an expensive operation.
      Parameters:
      value - the value to search for
      Returns:
      a key that maps to value, if present, or null if value cannot be found
    • ensureCapacity

      public void ensureCapacity(int additionalCapacity)
      Increases the size of the backing array to accommodate the specified number of additional items / loadFactor. Useful before adding many items to avoid multiple backing array resizes.
      Parameters:
      additionalCapacity - how many more items this must be able to hold; the load factor increases the actual capacity change
    • resize

      protected void resize(int newSize)
    • getHashMultiplier

      public int getHashMultiplier()
      Gets the current hashMultiplier, used in place(Object) to mix hash codes. If setHashMultiplier(int) is never called, the hashMultiplier will always be drawn from Utilities.GOOD_MULTIPLIERS, with the index equal to 64 - shift.
      Returns:
      the current hashMultiplier
    • setHashMultiplier

      public void setHashMultiplier(int hashMultiplier)
      Sets the hashMultiplier to the given int, which will be made odd if even and always negative (by OR-ing with 0x80000001). This can be any negative, odd int, but should almost always be drawn from Utilities.GOOD_MULTIPLIERS or something like it.
      Parameters:
      hashMultiplier - any int; will be made odd if even.
    • getTableSize

      public int getTableSize()
      Gets the length of the internal array used to store all keys, as well as empty space awaiting more items to be entered. This length is equal to the length of the array used to store all values, and empty space for values, here. This is also called the capacity.
      Returns:
      the length of the internal array that holds all keys
    • getLoadFactor

      public float getLoadFactor()
    • setLoadFactor

      public void setLoadFactor(float loadFactor)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString(String entrySeparator)
      Delegates to toString(String, boolean) with the given entrySeparator and without braces. This is different from toString(), which includes braces by default.
      Parameters:
      entrySeparator - how to separate entries, such as ", "
      Returns:
      a new String representing this map
    • toString

      public String toString(String entrySeparator, boolean braces)
    • toString

      public String toString(String entrySeparator, String keyValueSeparator, boolean braces, Appender<K> keyAppender, IntAppender valueAppender)
      Makes a String from the contents of this ObjectIntMap, but uses the given Appender and IntAppender to convert each key and each value to a customizable representation and append them to a temporary StringBuilder. These functions are often method references to methods in Base, such as Base.appendUnsigned(CharSequence, int). To use the default String representation, you can use Appender::append as an appender. To write numeric values so that they can be read back as Java source code, use Base::appendReadable for the valueAppender.
      Parameters:
      entrySeparator - how to separate entries, such as ", "
      keyValueSeparator - how to separate each key from its value, such as "=" or ":"
      braces - true to wrap the output in curly braces, or false to omit them
      keyAppender - a function that takes a StringBuilder and a K, and returns the modified StringBuilder
      valueAppender - a function that takes a StringBuilder and an int, and returns the modified StringBuilder
      Returns:
      a new String representing this map
    • appendTo

      public StringBuilder appendTo(StringBuilder sb, String entrySeparator, boolean braces)
    • appendTo

      public StringBuilder appendTo(StringBuilder sb, String entrySeparator, String keyValueSeparator, boolean braces, Appender<K> keyAppender, IntAppender valueAppender)
      Appends to a StringBuilder from the contents of this ObjectIntMap, but uses the given Appender and IntAppender to convert each key and each value to a customizable representation and append them to a StringBuilder. These functions are often method references to methods in Base, such as Base.appendUnsigned(CharSequence, int) . To use the default String representation, you can use Appender::append as an appender. To write numeric values so that they can be read back as Java source code, use IntAppender.READABLE for the valueAppender.
      Parameters:
      sb - a StringBuilder that this can append to
      entrySeparator - how to separate entries, such as ", "
      keyValueSeparator - how to separate each key from its value, such as "=" or ":"
      braces - true to wrap the output in curly braces, or false to omit them
      keyAppender - a function that takes a StringBuilder and a K, and returns the modified StringBuilder
      valueAppender - a function that takes a StringBuilder and an int, and returns the modified StringBuilder
      Returns:
      sb, with the appended keys and values of this map
    • forEach

      public void forEach(com.github.tommyettinger.function.ObjIntBiConsumer<? super K> action)
      Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of entry set iteration (if an iteration order is specified.) Exceptions thrown by the action are relayed to the caller.
      Parameters:
      action - The action to be performed for each entry
    • replaceAll

      public void replaceAll(com.github.tommyettinger.function.ObjIntToIntBiFunction<? super K> function)
      Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. Exceptions thrown by the function are relayed to the caller.
      Parameters:
      function - the function to apply to each entry
    • truncate

      public void truncate(int newSize)
      Reduces the size of the map to the specified size. If the map is already smaller than the specified size, no action is taken. This indiscriminately removes items from the backing array until the requested newSize is reached, or until the full backing array has had its elements removed.
      This tries to remove from the end of the iteration order, but because the iteration order is not guaranteed by an unordered map, this can remove essentially any item(s) from the map if it is larger than newSize.
      Parameters:
      newSize - the target size to try to reach by removing items, if smaller than the current size
    • iterator

      public ObjectIntMap.EntryIterator<K> iterator()
      Reuses the iterator of the reused ObjectIntMap.Entries produced by entrySet(); does not permit nested iteration. Iterate over Entries(ObjectIntMap) if you need nested or multithreaded iteration. You can remove an Entry from this ObjectIntMap using this Iterator.
      Specified by:
      iterator in interface Iterable<K>
      Returns:
      an Iterator over ObjectIntMap.Entry key-value pairs; remove is supported.
    • keySet

      public ObjectIntMap.Keys<K> keySet()
      Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

      Note that the same Collection instance is returned each time this method is called. Use the ObjectIntMap.Keys constructor for nested or multithreaded iteration.

      Returns:
      a set view of the keys contained in this map
    • values

      public ObjectIntMap.Values<K> values()
      Returns a Collection of the values in the map. Remove is supported. Note that the same Collection instance is returned each time this method is called. Use the ObjectIntMap.Values constructor for nested or multithreaded iteration.
      Returns:
      a Collection of int values
    • entrySet

      public ObjectIntMap.Entries<K> entrySet()
      Returns a Set of Entry, containing the entries in the map. Remove is supported by the Set's iterator. Note that the same iterator instance is returned each time this method is called. Use the ObjectIntMap.Entries constructor for nested or multithreaded iteration.
      Returns:
      a Set of ObjectIntMap.Entry key-value pairs
    • putIfAbsent

      public int putIfAbsent(K key, int value)
    • replace

      public boolean replace(K key, int oldValue, int newValue)
    • replace

      public int replace(K key, int value)
    • computeIfAbsent

      public int computeIfAbsent(K key, com.github.tommyettinger.function.ObjToIntFunction<? super K> mappingFunction)
    • remove

      public boolean remove(Object key, int value)
    • combine

      public int combine(K key, int value, com.github.tommyettinger.function.IntIntToIntBiFunction remappingFunction)
      Just like Map's merge() default method, but this doesn't use Java 8 APIs (so it should work on RoboVM), this uses primitive values, and this won't remove entries if the remappingFunction returns null (because that isn't possible with primitive types). This uses a functional interface from Funderby.
      Parameters:
      key - key with which the resulting value is to be associated
      value - the value to be merged with the existing value associated with the key or, if no existing value is associated with the key, to be associated with the key
      remappingFunction - given an int from this and the int value, this should return what int to use
      Returns:
      the value now associated with key
    • combine

      public void combine(ObjectIntMap<? extends K> other, com.github.tommyettinger.function.IntIntToIntBiFunction remappingFunction)
      Simply calls combine(Object, int, IntIntToIntBiFunction) on this map using every key-value pair in other. If other isn't empty, calling this will probably modify this map, though this depends on the remappingFunction.
      Parameters:
      other - a non-null ObjectIntMap (or subclass) with a compatible key type
      remappingFunction - given an int value from this and a value from other, this should return what int to use
    • with

      public static <K> ObjectIntMap<K> with()
      Constructs an empty map given the key type as a generic type argument. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.
      Type Parameters:
      K - the type of keys
      Returns:
      a new map containing nothing
    • with

      public static <K> ObjectIntMap<K> with(K key0, Number value0)
      Constructs a single-entry map given one key and one value. This is mostly useful as an optimization for with(Object, Number, Object...) when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number value to a primitive int, regardless of which Number type was used.
      Type Parameters:
      K - the type of key0
      Parameters:
      key0 - the first and only key
      value0 - the first and only value; will be converted to primitive int
      Returns:
      a new map containing just the entry mapping key0 to value0
    • with

      public static <K> ObjectIntMap<K> with(K key0, Number value0, K key1, Number value1)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Object, Number, Object...) when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number values to primitive ints, regardless of which Number type was used.
      Type Parameters:
      K - the type of keys
      Parameters:
      key0 - a K key
      value0 - a Number for a value; will be converted to primitive int
      key1 - a K key
      value1 - a Number for a value; will be converted to primitive int
      Returns:
      a new map containing the given key-value pairs
    • with

      public static <K> ObjectIntMap<K> with(K key0, Number value0, K key1, Number value1, K key2, Number value2)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Object, Number, Object...) when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number values to primitive ints, regardless of which Number type was used.
      Type Parameters:
      K - the type of keys
      Parameters:
      key0 - a K key
      value0 - a Number for a value; will be converted to primitive int
      key1 - a K key
      value1 - a Number for a value; will be converted to primitive int
      key2 - a K key
      value2 - a Number for a value; will be converted to primitive int
      Returns:
      a new map containing the given key-value pairs
    • with

      public static <K> ObjectIntMap<K> with(K key0, Number value0, K key1, Number value1, K key2, Number value2, K key3, Number value3)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Object, Number, Object...) when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number values to primitive ints, regardless of which Number type was used.
      Type Parameters:
      K - the type of keys
      Parameters:
      key0 - a K key
      value0 - a Number for a value; will be converted to primitive int
      key1 - a K key
      value1 - a Number for a value; will be converted to primitive int
      key2 - a K key
      value2 - a Number for a value; will be converted to primitive int
      key3 - a K key
      value3 - a Number for a value; will be converted to primitive int
      Returns:
      a new map containing the given key-value pairs
    • with

      public static <K> ObjectIntMap<K> with(K key0, Number value0, Object... rest)
      Constructs a map given alternating keys and values. This can be useful in some code-generation scenarios, or when you want to make a map conveniently by-hand and have it populated at the start. You can also use ObjectIntMap(Object[], int[]), which takes all keys and then all values. This needs all keys to have the same type, because it gets a generic type from the first key parameter. All values must be some type of boxed Number, such as Integer or Double, and will be converted to primitive ints. Any keys that don't have K as their type or values that aren't Numbers have that entry skipped.
      Type Parameters:
      K - the type of keys, inferred from key0
      Parameters:
      key0 - the first key; will be used to determine the type of all keys
      value0 - the first value; will be converted to primitive int
      rest - a varargs or non-null array of alternating K, Number, K, Number... elements
      Returns:
      a new map containing the given keys and values
    • putPairs

      public void putPairs(Object... pairs)
      Attempts to put alternating key-value pairs into this map, drawing a key, then a value from pairs, then another key, another value, and so on until another pair cannot be drawn. All values must be some type of boxed Number, such as Integer or Double, and will be converted to primitive ints. Any keys that don't have K as their type or values that aren't Numbers have that entry skipped.
      If any item in pairs cannot be cast to the appropriate K or Number type for its position in the arguments, that pair is ignored and neither that key nor value is put into the map. If any key is null, that pair is ignored, as well. If pairs is an Object array that is null, the entire call to putPairs() is ignored. If the length of pairs is odd, the last item (which will be unpaired) is ignored.
      Parameters:
      pairs - an array or varargs of alternating K, Number, K, Number... elements
    • putLegible

      public void putLegible(String str, PartialParser<K> keyParser)
      Adds items to this map drawn from the result of toString(String) or appendTo(StringBuilder, String, boolean). Every key-value pair should be separated by ", ", and every key should be followed by "=" before the value (which toString() does). A PartialParser will be used to parse keys from sections of str, and values are parsed with Base.readInt(CharSequence, int, int). Any brackets inside the given range of characters will ruin the parsing, so increase offset by 1 and reduce length by 2 if the original String had brackets added to it.
      Parameters:
      str - a String containing parseable text
      keyParser - a PartialParser that returns a K key from a section of str
    • putLegible

      public void putLegible(String str, String entrySeparator, PartialParser<K> keyParser)
      Adds items to this map drawn from the result of toString(String) or appendTo(StringBuilder, String, boolean). Every key-value pair should be separated by entrySeparator, and every key should be followed by "=" before the value (which toString(String) does). A PartialParser will be used to parse keys from sections of str, and values are parsed with Base.readInt(CharSequence, int, int). Any brackets inside the given range of characters will ruin the parsing, so increase offset by 1 and reduce length by 2 if the original String had brackets added to it.
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyParser - a PartialParser that returns a K key from a section of str
    • putLegible

      public void putLegible(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser)
      Adds items to this map drawn from the result of toString(String) or appendTo(StringBuilder, String, String, boolean, Appender, IntAppender). A PartialParser will be used to parse keys from sections of str, and values are parsed with Base.readInt(CharSequence, int, int). Any brackets inside the given range of characters will ruin the parsing, so increase offset by 1 and reduce length by 2 if the original String had brackets added to it.
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyValueSeparator - the String separating every key from its corresponding value
      keyParser - a PartialParser that returns a K key from a section of str
    • putLegible

      public void putLegible(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, int offset, int length)
      Puts key-value pairs into this map drawn from the result of toString(String) or appendTo(StringBuilder, String, String, boolean, Appender, IntAppender). A PartialParser will be used to parse keys from sections of str, and values are parsed with Base.readInt(CharSequence, int, int). Any brackets inside the given range of characters will ruin the parsing, so increase offset by 1 and reduce length by 2 if the original String had brackets added to it.
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyValueSeparator - the String separating every key from its corresponding value
      keyParser - a PartialParser that returns a K key from a section of str
      offset - the first position to read parseable text from in str
      length - how many chars to read; -1 is treated as maximum length
    • withPrimitive

      public static <K> ObjectIntMap<K> withPrimitive()
      Constructs an empty map given the key type as a generic type argument. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.
      Type Parameters:
      K - the type of keys
      Returns:
      a new map containing nothing
    • withPrimitive

      public static <K> ObjectIntMap<K> withPrimitive(K key0, int value0)
      Constructs a single-entry map given one key and one value. This is mostly useful as an optimization for with(Object, Number, Object...) when there's no "rest" of the keys or values. Unlike with(), this takes unboxed int as its value type, and will not box it.
      Type Parameters:
      K - the type of key0
      Parameters:
      key0 - a K for a key
      value0 - a int for a value
      Returns:
      a new map containing just the entry mapping key0 to value0
    • withPrimitive

      public static <K> ObjectIntMap<K> withPrimitive(K key0, int value0, K key1, int value1)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Object, Number, Object...) when there's no "rest" of the keys or values. Unlike with(), this takes unboxed int as its value type, and will not box it.
      Type Parameters:
      K - the type of keys
      Parameters:
      key0 - a K key
      value0 - a int for a value
      key1 - a K key
      value1 - a int for a value
      Returns:
      a new map containing the given key-value pairs
    • withPrimitive

      public static <K> ObjectIntMap<K> withPrimitive(K key0, int value0, K key1, int value1, K key2, int value2)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Object, Number, Object...) when there's no "rest" of the keys or values. Unlike with(), this takes unboxed int as its value type, and will not box it.
      Type Parameters:
      K - the type of keys
      Parameters:
      key0 - a K key
      value0 - a int for a value
      key1 - a K key
      value1 - a int for a value
      key2 - a K key
      value2 - a int for a value
      Returns:
      a new map containing the given key-value pairs
    • withPrimitive

      public static <K> ObjectIntMap<K> withPrimitive(K key0, int value0, K key1, int value1, K key2, int value2, K key3, int value3)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Object, Number, Object...) when there's no "rest" of the keys or values. Unlike with(), this takes unboxed int as its value type, and will not box it.
      Type Parameters:
      K - the type of keys
      Parameters:
      key0 - a K key
      value0 - a int for a value
      key1 - a K key
      value1 - a int for a value
      key2 - a K key
      value2 - a int for a value
      key3 - a K key
      value3 - a int for a value
      Returns:
      a new map containing the given key-value pairs
    • parse

      public static <K> ObjectIntMap<K> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser)
      Creates a new map by parsing all of str with the given PartialParser for keys, with entries separated by entrySeparator, such as ", " and the keys separated from values by keyValueSeparator, such as "=".
      Various PartialParser instances are defined as constants, such as PartialParser.DEFAULT_STRING, and others can be created by static methods in PartialParser, such as PartialParser.objectListParser(PartialParser, String, boolean).
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyValueSeparator - the String separating every key from its corresponding value
      keyParser - a PartialParser that returns a K key from a section of str
    • parse

      public static <K> ObjectIntMap<K> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, boolean brackets)
      Creates a new map by parsing all of str (or if brackets is true, all but the first and last chars) with the given PartialParser for keys, with entries separated by entrySeparator, such as ", " and the keys separated from values by keyValueSeparator, such as "=".
      Various PartialParser instances are defined as constants, such as PartialParser.DEFAULT_STRING, and others can be created by static methods in PartialParser, such as PartialParser.objectListParser(PartialParser, String, boolean).
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyValueSeparator - the String separating every key from its corresponding value
      keyParser - a PartialParser that returns a K key from a section of str
      brackets - if true, the first and last chars in str will be ignored
    • parse

      public static <K> ObjectIntMap<K> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, int offset, int length)
      Creates a new map by parsing the given subrange of str with the given PartialParser for keys, with entries separated by entrySeparator, such as ", " and the keys separated from values by keyValueSeparator, such as "=".
      Various PartialParser instances are defined as constants, such as PartialParser.DEFAULT_STRING, and others can be created by static methods in PartialParser, such as PartialParser.objectListParser(PartialParser, String, boolean).
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyValueSeparator - the String separating every key from its corresponding value
      keyParser - a PartialParser that returns a K key from a section of str
      offset - the first position to read parseable text from in str
      length - how many chars to read; -1 is treated as maximum length