Class ObjectObjectMap<K,V>
- Direct Known Subclasses:
CaseInsensitiveMap,FilteredIterableMap,FilteredStringMap,IdentityObjectMap,ObjectObjectOrderedMap
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classstatic classstatic classstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionReturned byget(Object)when no value exists for the given key, as well as some other methods to indicate that no value in the Map could be returned.protected ObjectObjectMap.Entries<K,V> protected ObjectObjectMap.Entries<K,V> protected intUsed byplace(Object)to mix hashCode() results.protected ObjectObjectMap.Keys<K,V> protected ObjectObjectMap.Keys<K,V> protected K[]protected floatBetween 0f (exclusive) and 1f (inclusive, if you're careful), this determines how full the backing tables can get before this increases their size.protected intA bitmask used to confine hashcodes to the size of the table.protected intUsed byplace(Object)typically, this should always equalBitConversion.countLeadingZeros(mask).protected intprotected intPrecalculated value of(int)(keyTable.length * loadFactor), used to determine when to resize.protected ObjectObjectMap.Values<K,V> protected ObjectObjectMap.Values<K,V> protected V[] -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor().ObjectObjectMap(int initialCapacity) Creates a new map with the given starting capacity and a load factor ofUtilities.getDefaultLoadFactor().ObjectObjectMap(int initialCapacity, float loadFactor) Creates a new map with the specified initial capacity and load factor.ObjectObjectMap(ObjectObjectMap<? extends K, ? extends V> map) Creates a new map identical to the specified map.ObjectObjectMap(Collection<? extends K> keys, Collection<? extends V> 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.ObjectObjectMap(Map<? extends K, ? extends V> map) Creates a new map identical to the specified map.ObjectObjectMap(K[] keys, V[] 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. -
Method Summary
Modifier and TypeMethodDescriptionappendTo(StringBuilder sb, String entrySeparator, boolean braces) appendTo(StringBuilder sb, String entrySeparator, String keyValueSeparator, boolean braces, Appender<K> keyAppender, Appender<V> valueAppender) voidclear()voidclear(int maximumCapacity) Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.voidcombine(Map<? extends K, ? extends V> other, com.github.tommyettinger.function.ObjObjToObjBiFunction<? super V, ? super V, ? extends V> remappingFunction) Simply callscombine(Object, Object, ObjObjToObjBiFunction)on this map using every key-value pair inother.combine(K key, V value, com.github.tommyettinger.function.ObjObjToObjBiFunction<? super V, ? super V, ? extends V> remappingFunction) Just like Map's merge() default method, but this doesn't use Java 8 APIs (so it should work on RoboVM), and this won't remove entries if the remappingFunction returns null (in that case, it will callput(key, null)).booleancontainsKey(Object key) booleancontainsValue(Object value) Returnstrueif this map maps one or more keys to the specified value.booleancontainsValue(Object value, boolean identity) Returns true if the specified value is in the map.voidensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items / loadFactor.entrySet()Returns a Set of Map.Entry, containing the entries in the map.booleanbooleanequalsIdentity(Object obj) Uses == for comparison of each value.protected booleanCompares the objects left and right, which are usually keys, for equality, returning true if they are considered equal.Returns a key that maps to the specified value, or null if value is not in the map.Returns the key for the specified value, or null if it is not in the map.Returns the value for the specified key, ordefaultValueif the key is not in the map.Gets the default value, aVwhich is returned byget(Object)if the key is not found.intGets the current hashMultiplier, used inplace(Object)to mix hash codes.floatgetOrDefault(Object key, V defaultValue) Returns the value for the specified key, or the given default value if the key is not in the map.intGets the length of the internal array used to store all keys, as well as empty space awaiting more items to be entered.inthashCode()booleanisEmpty()Returns true if the map is empty.iterator()Reuses the iterator of the reusedObjectObjectMap.Entriesproduced byentrySet(); does not permit nested iteration.keySet()Returns aSetview of the keys contained in this map.protected intReturns the index of the key if already present, else~indexfor the next empty index.booleannotEmpty()Returns true if the map has one or more items.static <K,V> ObjectObjectMap<K, V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser) Creates a new map by parsing all ofstrwith the given PartialParser for keys and for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".static <K,V> ObjectObjectMap<K, V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser, boolean brackets) Creates a new map by parsing all ofstr(or ifbracketsis true, all but the first and last chars) with the given PartialParser for keys and for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".static <K,V> ObjectObjectMap<K, V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser, int offset, int length) Creates a new map by parsing the given subrange ofstrwith the given PartialParser for keys and for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".protected intReturns an index >= 0 and <=maskfor the specifieditem, mixed.Returns the old value associated with the specified key, or this map'sdefaultValueif there was no prior value.voidputAll(ObjectObjectMap<? extends K, ? extends V> 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.voidputAll(Collection<? extends K> keys, Collection<? extends V> 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().voidCopies all the mappings from the specified map to this map (optional operation).voidGiven two side-by-side arrays, one of keys, one of values, this inserts each pair of key and value into this map with put().voidGiven two side-by-side arrays, one of keys, one of values, this inserts each pair of key and value into this map with put().voidGiven two side-by-side arrays, one of keys, one of values, this inserts each pair of key and value into this map with put().voidputLegible(String str, PartialParser<K> keyParser, PartialParser<V> valueParser) Adds items to this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, boolean).voidputLegible(String str, String entrySeparator, PartialParser<K> keyParser, PartialParser<V> valueParser) Adds items to this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, boolean).voidputLegible(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser) Adds items to this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, String, boolean, Appender, Appender).voidputLegible(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser, int offset, int length) Puts key-value pairs into this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, String, boolean, Appender, Appender).putOrDefault(K key, V value, V defaultValue) voidAttempts to put alternating key-value pairs into this map, drawing a key, then a value frompairs, then another key, another value, and so on until another pair cannot be drawn.protected voidSkips checks for existing keys, doesn't increment size.protected voidresize(int newSize) voidsetDefaultValue(V defaultValue) Sets the default value, aVwhich is returned byget(Object)if the key is not found.voidsetHashMultiplier(int hashMultiplier) Sets the hashMultiplier to the given int, which will be made odd if even and always negative (by OR-ing with 0x80000001).voidsetLoadFactor(float loadFactor) voidshrink(int maximumCapacity) Reduces the size of the backing arrays to be the specified capacity / loadFactor, or less.intsize()Returns the number of key-value mappings in this map.toString()Delegates totoString(String, boolean)with the given entrySeparator and without braces.toString(String entrySeparator, String keyValueSeparator, boolean braces, Appender<K> keyAppender, Appender<V> valueAppender) voidtruncate(int newSize) Reduces the size of the map to the specified size.values()Returns a Collection of the values in the map.static <K,V> ObjectObjectMap<K, V> with()Constructs an empty map given the types as generic type arguments.static <K,V> ObjectObjectMap<K, V> with(K key0, V value0) Constructs a single-entry map given one key and one value.static <K,V> ObjectObjectMap<K, V> Constructs a map given alternating keys and values.static <K,V> ObjectObjectMap<K, V> with(K key0, V value0, K key1, V value1) Constructs a single-entry map given two key-value pairs.static <K,V> ObjectObjectMap<K, V> with(K key0, V value0, K key1, V value1, K key2, V value2) Constructs a single-entry map given three key-value pairs.static <K,V> ObjectObjectMap<K, V> with(K key0, V value0, K key1, V value1, K key2, V value2, K key3, V value3) Constructs a single-entry map given four key-value pairs.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, merge, putIfAbsent, remove, replace, replaceAll
-
Field Details
-
size
protected int size -
keyTable
-
valueTable
-
loadFactor
protected float loadFactorBetween 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 thresholdPrecalculated value of(int)(keyTable.length * loadFactor), used to determine when to resize. -
shift
protected int shiftUsed byplace(Object)typically, this should always equalBitConversion.countLeadingZeros(mask). For a table that could hold 2 items (with 1 bit indices), this would be64 - 1 == 63. For a table that could hold 256 items (with 8 bit indices), this would be64 - 8 == 56. -
mask
protected int maskA 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. Ifplace(Object)is overridden, this can be used instead ofshiftto isolate usable bits of a hash. -
hashMultiplier
protected int hashMultiplierUsed byplace(Object)to mix hashCode() results. Changes on every call toresize(int)by default. This should always change whenshiftchanges, 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 usingObjectObjectOrderedMap. -
entries1
-
entries2
-
values1
-
values2
-
keys1
-
keys2
-
defaultValue
Returned byget(Object)when no value exists for the given key, as well as some other methods to indicate that no value in the Map could be returned.
-
-
Constructor Details
-
ObjectObjectMap
public ObjectObjectMap()Creates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor(). -
ObjectObjectMap
public ObjectObjectMap(int initialCapacity) Creates a new map with the given starting capacity and a load factor ofUtilities.getDefaultLoadFactor().- Parameters:
initialCapacity- If not a power of two, it is increased to the next nearest power of two.
-
ObjectObjectMap
public ObjectObjectMap(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
-
ObjectObjectMap
Creates a new map identical to the specified map.- Parameters:
map- an ObjectObjectMap to copy
-
ObjectObjectMap
Creates a new map identical to the specified map.- Parameters:
map- a Map to copy; ObjectObjectMap or its subclasses will be faster
-
ObjectObjectMap
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 keysvalues- an array of values
-
ObjectObjectMap
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 keysvalues- a Collection of values
-
-
Method Details
-
place
Returns an index >= 0 and <=maskfor the specifieditem, 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
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 returnsleft.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 necessarilyright- 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
Returns the index of the key if already present, else~indexfor the next empty index. This callsequate(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
Returns the old value associated with the specified key, or this map'sdefaultValueif there was no prior value. -
putOrDefault
-
putAll
Copies all the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of callingput(k, v)on this map once for each mapping from keykto valuevin the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
Note thatputAll(ObjectObjectMap)is more specific and can be more efficient by using the internal details of this class.- Specified by:
putAllin interfaceMap<K,V> - Parameters:
m- mappings to be stored in this map- Throws:
UnsupportedOperationException- if theputAlloperation is not supported by this mapClassCastException- if the class of a key or value in the specified map prevents it from being stored in this mapNullPointerException- if the specified map is null, or if this map does not permit null keys or values, and the specified map contains null keys or valuesIllegalArgumentException- if some property of a key or value in the specified map prevents it from being stored in this map
-
putAll
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
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 keysvalues- a Collection of values
-
putAll
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 keysvalues- an array of values
-
putAll
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 keysvalues- an array of valueslength- how many items from keys and values to insert, at-most
-
putAll
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 keyskeyOffset- the first index in keys to insertvalues- an array of valuesvalueOffset- the first index in values to insertlength- how many items from keys and values to insert, at-most
-
putResize
Skips checks for existing keys, doesn't increment size. -
get
Returns the value for the specified key, ordefaultValueif the key is not in the map. Note thatdefaultValueis often null, which is also a valid value that can be assigned to a legitimate key. Checking that the result of this method is null does not guarantee that thekeyis not present. -
getOrDefault
Returns the value for the specified key, or the given default value if the key is not in the map.- Specified by:
getOrDefaultin interfaceMap<K,V>
-
remove
-
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 thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE. -
isEmpty
public boolean isEmpty()Returns true if the map is empty. -
getDefaultValue
Gets the default value, aVwhich is returned byget(Object)if the key is not found. If not changed, the default value is null.- Returns:
- the current default value
-
setDefaultValue
Sets the default value, aVwhich is returned byget(Object)if the key is not found. If not changed, the default value is null. Note thatgetOrDefault(Object, Object)is also available, which allows specifying a "not-found" value per-call.- Parameters:
defaultValue- may be any V object or null; 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
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.- Parameters:
identity- If true, uses == to compare the specified value with values in the map. If false, usesequals(Object).
-
containsKey
- Specified by:
containsKeyin interfaceMap<K,V>
-
containsValue
Returnstrueif this map maps one or more keys to the specified value. More formally, returnstrueif and only if this map contains at least one mapping to a valuevsuch that(value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of theMapinterface.- Specified by:
containsValuein interfaceMap<K,V> - Parameters:
value- value whose presence in this map is to be tested- Returns:
trueif this map maps one or more keys to the specified value- Throws:
ClassCastException- if the value is of an inappropriate type for this map (optional)NullPointerException- if the specified value is null and this map does not permit null values (optional)
-
findKey
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 usingObject.equals(Object), which may be an expensive operation. This is the same as callingfindKey(value, false).- Parameters:
value- the value to search for- Returns:
- a key that maps to value, if present, or null if value cannot be found
-
findKey
Returns the key for the specified value, or null if it 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 foridentity- If true, uses == to compare the specified value with values in the map. If false, usesequals(Object).- 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 additional items this should be able to hold without resizing (probably)
-
resize
protected void resize(int newSize) -
getHashMultiplier
public int getHashMultiplier()Gets the current hashMultiplier, used inplace(Object)to mix hash codes. IfsetHashMultiplier(int)is never called, the hashMultiplier will always be drawn fromUtilities.GOOD_MULTIPLIERS, with the index equal to64 - 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 fromUtilities.GOOD_MULTIPLIERSor 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() -
equals
-
equalsIdentity
Uses == for comparison of each value. -
toString
-
toString
Delegates totoString(String, boolean)with the given entrySeparator and without braces. This is different fromtoString(), which includes braces by default.- Parameters:
entrySeparator- how to separate entries, such as", "- Returns:
- a new String representing this map
-
toString
-
toString
public String toString(String entrySeparator, String keyValueSeparator, boolean braces, Appender<K> keyAppender, Appender<V> valueAppender) Makes a String from the contents of this ObjectObjectMap, but uses the givenAppenderandAppenderto convert each key and each value to a customizable representation and append them to a temporary StringBuilder. To use the default String representation, you can useAppender::appendas an appender.- 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 themkeyAppender- a function that takes a StringBuilder and a K, and returns the modified StringBuildervalueAppender- a function that takes a StringBuilder and a V, and returns the modified StringBuilder- Returns:
- a new String representing this map
-
appendTo
-
appendTo
public StringBuilder appendTo(StringBuilder sb, String entrySeparator, String keyValueSeparator, boolean braces, Appender<K> keyAppender, Appender<V> valueAppender) Appends to a StringBuilder from the contents of this ObjectObjectMap, but uses the givenAppenderandAppenderto convert each key and each value to a customizable representation and append them to a StringBuilder. To use the default String representation, you can useAppender::appendas an appender.- Parameters:
sb- a StringBuilder that this can append toentrySeparator- 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 themkeyAppender- a function that takes a StringBuilder and a K, and returns the modified StringBuildervalueAppender- a function that takes a StringBuilder and a V, and returns the modified StringBuilder- Returns:
sb, with the appended keys and values of this map
-
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
-
replace
-
combine
public V combine(K key, V value, com.github.tommyettinger.function.ObjObjToObjBiFunction<? super V, ? super V, ? extends V> remappingFunction) Just like Map's merge() default method, but this doesn't use Java 8 APIs (so it should work on RoboVM), and this won't remove entries if the remappingFunction returns null (in that case, it will callput(key, null)). This also uses a functional interface from Funderby instead of the JDK, for RoboVM support.- Parameters:
key- key with which the resulting value is to be associatedvalue- 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 keyremappingFunction- given a V from this and the Vvalue, this should return what V to use- Returns:
- the value now associated with key
-
combine
public void combine(Map<? extends K, ? extends V> other, com.github.tommyettinger.function.ObjObjToObjBiFunction<? super V, ? super V, ? extends V> remappingFunction) Simply callscombine(Object, Object, ObjObjToObjBiFunction)on this map using every key-value pair inother. Ifotherisn't empty, calling this will probably modify this map, though this depends on theremappingFunction.- Parameters:
other- a non-null Map (or subclass) with compatible key and value typesremappingFunction- given a V value from this and a value from other, this should return what V to use
-
iterator
Reuses the iterator of the reusedObjectObjectMap.Entriesproduced byentrySet(); does not permit nested iteration. Iterate overEntries(ObjectObjectMap)if you need nested or multithreaded iteration. You can remove an Entry from this ObjectObjectMap using this Iterator. -
keySet
Returns aSetview 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 ownremoveoperation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via theIterator.remove,Set.remove,removeAll,retainAll, andclearoperations. It does not support theaddoraddAlloperations.Note that the same Collection instance is returned each time this method is called. Use the
ObjectObjectMap.Keysconstructor for nested or multithreaded iteration. -
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 theObjectObjectMap.Valuesconstructor for nested or multithreaded iteration.- Specified by:
valuesin interfaceMap<K,V> - Returns:
- a
Collectionof V values
-
entrySet
Returns a Set of Map.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 theObjectObjectMap.Entriesconstructor for nested or multithreaded iteration. -
with
Constructs an empty map given the types as generic type arguments. 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 keysV- the type of values- Returns:
- a new map containing nothing
-
with
Constructs a single-entry map given one key and one value. This is mostly useful as an optimization forwith(Object, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
K- the type of key0V- the type of value0- Parameters:
key0- the first and only keyvalue0- the first and only value- Returns:
- a new map containing just the entry mapping key0 to value0
-
with
Constructs a single-entry map given two key-value pairs. This is mostly useful as an optimization forwith(Object, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
K- the type of key0V- the type of value0- Parameters:
key0- a K keyvalue0- a V valuekey1- a K keyvalue1- a V value- Returns:
- a new map containing entries mapping each key to the following value
-
with
public static <K,V> ObjectObjectMap<K,V> with(K key0, V value0, K key1, V value1, K key2, V value2) Constructs a single-entry map given three key-value pairs. This is mostly useful as an optimization forwith(Object, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
K- the type of key0V- the type of value0- Parameters:
key0- a K keyvalue0- a V valuekey1- a K keyvalue1- a V valuekey2- a K keyvalue2- a V value- Returns:
- a new map containing entries mapping each key to the following value
-
with
public static <K,V> ObjectObjectMap<K,V> with(K key0, V value0, K key1, V value1, K key2, V value2, K key3, V value3) Constructs a single-entry map given four key-value pairs. This is mostly useful as an optimization forwith(Object, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
K- the type of key0V- the type of value0- Parameters:
key0- a K keyvalue0- a V valuekey1- a K keyvalue1- a V valuekey2- a K keyvalue2- a V valuekey3- a K keyvalue3- a V value- Returns:
- a new map containing entries mapping each key to the following value
-
with
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 useObjectObjectMap(Object[], Object[]), which takes all keys and then all values. This needs all keys to have the same type and all values to have the same type, because it gets those types from the first key parameter and first value parameter. Any keys that don't have K as their type or values that don't have V as their type have that entry skipped.- Type Parameters:
K- the type of keys, inferred from key0V- the type of values, inferred from value0- Parameters:
key0- the first key; will be used to determine the type of all keysvalue0- the first value; will be used to determine the type of all valuesrest- a varargs or non-null array of alternating K, V, K, V... elements- Returns:
- a new map containing the given keys and values
-
putPairs
Attempts to put alternating key-value pairs into this map, drawing a key, then a value frompairs, then another key, another value, and so on until another pair cannot be drawn. Any keys that don't have K as their type or values that don't have V as their type have that entry skipped.
If any item inpairscannot be cast to the appropriate K or V 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. Ifpairsis an Object array that is null, the entire call to putPairs() is ignored. If the length ofpairsis odd, the last item (which will be unpaired) is ignored.- Parameters:
pairs- an array or varargs of alternating K, V, K, V... elements
-
putLegible
Adds items to this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, boolean). Every key-value pair should be separated by", ", and every key should be followed by"="before the value (whichtoString()does). A PartialParser will be used to parse keys from sections ofstr, and a different PartialParser to parse values. 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 textkeyParser- a PartialParser that returns aKkey from a section ofstrvalueParser- a PartialParser that returns aVvalue from a section ofstr
-
putLegible
public void putLegible(String str, String entrySeparator, PartialParser<K> keyParser, PartialParser<V> valueParser) Adds items to this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, boolean). Every key-value pair should be separated byentrySeparator, and every key should be followed by "=" before the value (whichtoString(String)does). A PartialParser will be used to parse keys from sections ofstr, and a different PartialParser to parse values. 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 textentrySeparator- the String separating every key-value pairkeyParser- a PartialParser that returns aKkey from a section ofstrvalueParser- a PartialParser that returns aVvalue from a section ofstr
-
putLegible
public void putLegible(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser) Adds items to this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, String, boolean, Appender, Appender). A PartialParser will be used to parse keys from sections ofstr, and a different PartialParser to parse values. 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 textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valuekeyParser- a PartialParser that returns aKkey from a section ofstrvalueParser- a PartialParser that returns aVvalue from a section ofstr
-
putLegible
public void putLegible(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser, int offset, int length) Puts key-value pairs into this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, String, boolean, Appender, Appender). A PartialParser will be used to parse keys from sections ofstr, and a different PartialParser to parse values. 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 textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valuekeyParser- a PartialParser that returns aKkey from a section ofstrvalueParser- a PartialParser that returns aVvalue from a section ofstroffset- the first position to read parseable text from instrlength- how many chars to read; -1 is treated as maximum length
-
parse
public static <K,V> ObjectObjectMap<K,V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser) Creates a new map by parsing all ofstrwith the given PartialParser for keys and for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".
VariousPartialParserinstances are defined as constants, such asPartialParser.DEFAULT_STRING, and others can be created by static methods in PartialParser, such asPartialParser.objectListParser(PartialParser, String, boolean).- Parameters:
str- a String containing parseable textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valuekeyParser- a PartialParser that returns aKkey from a section ofstrvalueParser- a PartialParser that returns aVvalue from a section ofstr
-
parse
public static <K,V> ObjectObjectMap<K,V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser, boolean brackets) Creates a new map by parsing all ofstr(or ifbracketsis true, all but the first and last chars) with the given PartialParser for keys and for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".
VariousPartialParserinstances are defined as constants, such asPartialParser.DEFAULT_STRING, and others can be created by static methods in PartialParser, such asPartialParser.objectListParser(PartialParser, String, boolean).- Parameters:
str- a String containing parseable textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valuekeyParser- a PartialParser that returns aKkey from a section ofstrvalueParser- a PartialParser that returns aVvalue from a section ofstrbrackets- if true, the first and last chars instrwill be ignored
-
parse
public static <K,V> ObjectObjectMap<K,V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser, int offset, int length) Creates a new map by parsing the given subrange ofstrwith the given PartialParser for keys and for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".
VariousPartialParserinstances are defined as constants, such asPartialParser.DEFAULT_STRING, and others can be created by static methods in PartialParser, such asPartialParser.objectListParser(PartialParser, String, boolean).- Parameters:
str- a String containing parseable textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valuekeyParser- a PartialParser that returns aKkey from a section ofstrvalueParser- a PartialParser that returns aVvalue from a section ofstroffset- the first position to read parseable text from instrlength- how many chars to read; -1 is treated as maximum length
-