Class LongLongMap
- All Implemented Interfaces:
Iterable<LongLongMap.Entry>
- Direct Known Subclasses:
LongLongOrderedMap
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(long) can be overridden to change how hashCodes
are calculated (which can be useful for types like StringBuilder that don't implement hashCode()), and
locateKey(long) 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, just more slowly.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classstatic classstatic classstatic classstatic classstatic classstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionlongprotected LongLongMap.Entriesprotected LongLongMap.Entriesprotected intUsed byplace(long)to mix hashCode() results.protected booleanprotected LongLongMap.Keysprotected LongLongMap.Keysprotected long[]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 tables.protected intprotected intprotected intPrecalculated value of(int)(keyTable.length * loadFactor), used to determine when to resize.protected LongLongMap.Valuesprotected LongLongMap.Valuesprotected long[]protected long -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor().LongLongMap(int initialCapacity) Creates a new map with the given starting capacity and a load factor ofUtilities.getDefaultLoadFactor().LongLongMap(int initialCapacity, float loadFactor) Creates a new map with the specified initial capacity and load factor.LongLongMap(long[] keys, long[] 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.LongLongMap(LongLongMap map) Creates a new map identical to the specified map.LongLongMap(PrimitiveCollection.OfLong keys, PrimitiveCollection.OfLong 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. -
Method Summary
Modifier and TypeMethodDescriptionappendTo(StringBuilder sb, String entrySeparator, boolean braces) appendTo(StringBuilder sb, String entrySeparator, String keyValueSeparator, boolean braces, LongAppender keyAppender, LongAppender valueAppender) Appends to a StringBuilder from the contents of this LongLongMap, but uses the givenLongAppenderandLongAppenderto convert each key and each value to a customizable representation and append them to a StringBuilder.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.longcombine(long key, long value, com.github.tommyettinger.function.LongLongToLongBiFunction 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).voidcombine(LongLongMap other, com.github.tommyettinger.function.LongLongToLongBiFunction remappingFunction) Simply callscombine(long, long, LongLongToLongBiFunction)on this map using every key-value pair inother.longcomputeIfAbsent(long key, com.github.tommyettinger.function.LongToLongFunction mappingFunction) booleancontainsKey(long key) booleancontainsValue(long value) 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 Entry, containing the entries in the map.booleanlongfindKey(long value, long defaultKey) Returns a key that maps to the specified value, ordefaultKeyif value is not in the map.voidforEach(com.github.tommyettinger.function.LongLongBiConsumer action) Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.longget(long key) Returns the value for the specified key, ordefaultValueif the key is not in the map.longgetAndIncrement(long key, long defaultValue, long increment) Returns the key's current value and increments the stored value.longGets the default value, alongwhich is returned byget(long)if the key is not found.intGets the current hashMultiplier, used inplace(long)to mix hash codes.floatlonggetOrDefault(long key, long defaultValue) Returns the value for the specified key, or the 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 reusedLongLongMap.Entriesproduced byentrySet(); does not permit nested iteration.keySet()Returns aSetview of the keys contained in this map.protected intlocateKey(long key) Returns 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 LongLongMapCreates a new map by parsing all ofstr, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".static LongLongMapCreates a new map by parsing all ofstr(or ifbracketsis true, all but the first and last chars), with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".static LongLongMapCreates a new map by parsing the given subrange ofstr, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".protected intplace(long item) Returns an index >= 0 and <=maskfor the specifieditem.longput(long key, long value) Returns the old value associated with the specified key, or this map'sdefaultValueif there was no prior value.voidputAll(long[] keys, int keyOffset, long[] 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().voidputAll(long[] keys, long[] 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().voidputAll(long[] keys, long[] 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().voidputAll(LongLongMap 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(PrimitiveCollection.OfLong keys, PrimitiveCollection.OfLong 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().longputIfAbsent(long key, long value) voidputLegible(String str) Adds items to this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, boolean).voidputLegible(String str, String entrySeparator) Adds items to this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, boolean).voidputLegible(String str, String entrySeparator, String keyValueSeparator) Adds items to this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, String, boolean, LongAppender, LongAppender).voidputLegible(String str, String entrySeparator, String keyValueSeparator, int offset, int length) Puts key-value pairs into this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, String, boolean, LongAppender, LongAppender).longputOrDefault(long key, long value, long defaultValue) Returns the old value associated with the specified key, or the givendefaultValueif there was no prior value.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.voidputPairsPrimitive(long... pairs) 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.protected voidputResize(long key, long value) Skips checks for existing keys, doesn't increment size.longremove(long key) booleanremove(long key, long value) longreplace(long key, long value) booleanreplace(long key, long oldValue, long newValue) voidreplaceAll(com.github.tommyettinger.function.LongLongToLongBiFunction 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.protected voidresize(int newSize) voidsetDefaultValue(long defaultValue) Sets the default value, alongwhich is returned byget(long)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, LongAppender keyAppender, LongAppender valueAppender) Makes a String from the contents of this LongLongMap, but uses the givenLongAppenderandLongAppenderto convert each key and each value to a customizable representation and append them to a temporary StringBuilder.voidtruncate(int newSize) Reduces the size of the map to the specified size.values()Returns a Collection of the values in the map.static LongLongMapwith()Constructs an empty map.static LongLongMapConstructs a single-entry map given one key and one value.static LongLongMapConstructs a map given alternating keys and values.static LongLongMapConstructs a map given alternating keys and values.static LongLongMapConstructs a map given alternating keys and values.static LongLongMapwith(Number key0, Number value0, Number key1, Number value1, Number key2, Number value2, Number key3, Number value3) Constructs a map given alternating keys and values.static LongLongMapConstructs an empty map.static LongLongMapwithPrimitive(long key0, long value0) Constructs a single-entry map given one key and one value.static LongLongMapwithPrimitive(long key0, long value0, long... rest) Constructs a map given alternating keys and values.static LongLongMapwithPrimitive(long key0, long value0, long key1, long value1) Constructs a map given alternating keys and values.static LongLongMapwithPrimitive(long key0, long value0, long key1, long value1, long key2, long value2) Constructs a map given alternating keys and values.static LongLongMapwithPrimitive(long key0, long value0, long key1, long value1, long key2, long value2, long key3, long value3) Constructs a map given alternating keys and values.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
size
protected int size -
keyTable
protected long[] keyTable -
valueTable
protected long[] valueTable -
hasZeroValue
protected boolean hasZeroValue -
zeroValue
protected long zeroValue -
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(long)to bit shift the upper bits of anintinto a usable range (>= 0 and <=mask). The shift can be negative, which is convenient to match the number of bits in mask: if mask is a 7-bit number, a shift of -7 shifts the upper 7 bits into the lowest 7 positions. This class sets the shift > 32 and < 64, which when used with an int will still move the upper bits of an int to the lower bits due to Java's implicit modulus on shifts.maskcan also be used to mask the low bits of a number, which may be faster for some hashcodes, ifplace(long)is overridden. -
mask
protected int maskA bitmask used to confine hashcodes to the size of the tables. Must be all 1 bits in its low positions, ie a power of two minus 1. Ifplace(long)is overridden, this can be used instead ofshiftto isolate usable bits of a hash. -
hashMultiplier
protected int hashMultiplierUsed byplace(long)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 usingLongLongOrderedMap. -
entries1
-
entries2
-
values1
-
values2
-
keys1
-
keys2
-
defaultValue
public long defaultValue
-
-
Constructor Details
-
LongLongMap
public LongLongMap()Creates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor(). -
LongLongMap
public LongLongMap(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.
-
LongLongMap
public LongLongMap(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
-
LongLongMap
Creates a new map identical to the specified map.- Parameters:
map- the map to copy
-
LongLongMap
public LongLongMap(long[] keys, long[] 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 keysvalues- an array of values
-
LongLongMap
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 PrimitiveCollection of keysvalues- a PrimitiveCollection of values
-
-
Method Details
-
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 PrimitiveCollection of keysvalues- a PrimitiveCollection of values
-
place
protected int place(long item) Returns an index >= 0 and <=maskfor the specifieditem.- Parameters:
item- any long; it is usually mixed or masked here- Returns:
- an index between 0 and
mask(both inclusive)
-
locateKey
protected int locateKey(long key) Returns the index of the key if already present, else~indexfor the next empty index. While this can be overridden to compare for equality differently than==between ints, that isn't recommended because this has to treat zero keys differently, and it finds those with==. If you want to treat equality between longs differently for some reason, you would also need to overridecontainsKey(long)andget(long), at the very least. -
put
public long put(long key, long value) Returns the old value associated with the specified key, or this map'sdefaultValueif there was no prior value. -
putOrDefault
public long putOrDefault(long key, long value, long defaultValue) Returns the old value associated with the specified key, or the givendefaultValueif there was no prior value. -
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
public void putAll(long[] keys, long[] 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 keysvalues- an array of values
-
putAll
public void putAll(long[] keys, long[] 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 keysvalues- an array of valueslength- how many items from keys and values to insert, at-most
-
putAll
public void putAll(long[] keys, int keyOffset, long[] 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 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
protected void putResize(long key, long value) Skips checks for existing keys, doesn't increment size. -
get
public long get(long key) Returns the value for the specified key, ordefaultValueif the key is not in the map.- Parameters:
key- anylong
-
getOrDefault
public long getOrDefault(long key, long defaultValue) Returns the value for the specified key, or the default value if the key is not in the map. -
getAndIncrement
public long getAndIncrement(long key, long defaultValue, long 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 long remove(long 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 thanInteger.MAX_VALUEelements, returnsInteger.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 long getDefaultValue()Gets the default value, alongwhich is returned byget(long)if the key is not found. If not changed, the default value is 0.- Returns:
- the current default value
-
setDefaultValue
public void setDefaultValue(long defaultValue) Sets the default value, alongwhich is returned byget(long)if the key is not found. If not changed, the default value is 0. Note thatgetOrDefault(long, long)is also available, which allows specifying a "not-found" value per-call.- Parameters:
defaultValue- may be any long; 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(long 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(long key) -
findKey
public long findKey(long value, long defaultKey) Returns a key that maps to the specified value, ordefaultKeyif 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 fordefaultKey- the key to return when value cannot be found- Returns:
- a key that maps to value, if present, or defaultKey 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. -
resize
protected void resize(int newSize) -
getHashMultiplier
public int getHashMultiplier()Gets the current hashMultiplier, used inplace(long)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
-
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, LongAppender keyAppender, LongAppender valueAppender) Makes a String from the contents of this LongLongMap, but uses the givenLongAppenderandLongAppenderto 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 asBase.appendReadable(CharSequence, long)andBase.appendUnsigned(CharSequence, long). To use the default String representation, you can useLongAppender.DEFAULTas an appender. To write values so that they can be read back as Java source code, useLongAppender.READABLEfor each appender.
UsingREADABLEappenders, if you separate keys from values with", "and also separate entries with", ", that allows the output to be copied into source code that callswith(Number, Number, Number...)(ifbracesis false).- 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 long, and returns the modified StringBuildervalueAppender- a function that takes a StringBuilder and a long, 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, LongAppender keyAppender, LongAppender valueAppender) Appends to a StringBuilder from the contents of this LongLongMap, but uses the givenLongAppenderandLongAppenderto 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 asBase.appendReadable(CharSequence, long)andBase.appendUnsigned(CharSequence, long). To use the default String representation, you can useAppender::appendas an appender. To write values so that they can be read back as Java source code, useBase::appendReadablefor each appender.
UsingBase::appendReadable, if you separate keys from values with", "and also separate entries with", ", that allows the output to be copied into source code that callswith(Number, Number, Number...)(ifbracesis false).- 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 long, and returns the modified StringBuildervalueAppender- a function that takes a StringBuilder and a long, and returns the modified StringBuilder- Returns:
sb, with the appended keys and values of this map
-
forEach
public void forEach(com.github.tommyettinger.function.LongLongBiConsumer 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.LongLongToLongBiFunction 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
Reuses the iterator of the reusedLongLongMap.Entriesproduced byentrySet(); does not permit nested iteration. Iterate overEntries(LongLongMap)if you need nested or multithreaded iteration. You can remove an Entry from this LongLongMap using this Iterator.- Specified by:
iteratorin interfaceIterable<LongLongMap.Entry>- Returns:
- an
IteratoroverLongLongMap.Entrykey-value pairs; remove is supported.
-
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()operation. It does not support theadd,addAll,remove,removeAll, orclearoperations.Note that the same Collection instance is returned each time this method is called. Use the
LongLongMap.Keysconstructor for nested or multithreaded iteration.- Returns:
- a set view of the keys contained in this map
-
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 theLongLongMap.Valuesconstructor for nested or multithreaded iteration.- Returns:
- a
PrimitiveCollection.OfLongcontaininglongvalues
-
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 theLongLongMap.Entriesconstructor for nested or multithreaded iteration.- Returns:
- a
SetofLongLongMap.Entrykey-value pairs
-
putIfAbsent
public long putIfAbsent(long key, long value) -
replace
public boolean replace(long key, long oldValue, long newValue) -
replace
public long replace(long key, long value) -
computeIfAbsent
public long computeIfAbsent(long key, com.github.tommyettinger.function.LongToLongFunction mappingFunction) -
remove
public boolean remove(long key, long value) -
combine
public long combine(long key, long value, com.github.tommyettinger.function.LongLongToLongBiFunction 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 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 long from this and the longvalue, this should return what long to use- Returns:
- the value now associated with key
-
combine
public void combine(LongLongMap other, com.github.tommyettinger.function.LongLongToLongBiFunction remappingFunction) Simply callscombine(long, long, LongLongToLongBiFunction)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 LongLongMap (or subclass) with a compatible key typeremappingFunction- given a long value from this and a value from other, this should return what long to use
-
with
Constructs an empty map. 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.- 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(Number, Number, Number...)when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number keys and values to primitive long and long, regardless of which Number type was used.- Parameters:
key0- the first and only key; will be converted to primitive longvalue0- the first and only value; will be converted to primitive long- Returns:
- a new map containing just the entry mapping key0 to value0
-
with
Constructs a map given alternating keys and values. This is mostly useful as an optimization forwith(Number, Number, Number...)when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number keys and values to primitive long and long, regardless of which Number type was used.- Parameters:
key0- a Number key; will be converted to primitive longvalue0- a Number for a value; will be converted to primitive longkey1- a Number key; will be converted to primitive longvalue1- a Number for a value; will be converted to primitive long- Returns:
- a new map containing the given key-value pairs
-
with
public static LongLongMap with(Number key0, Number value0, Number key1, Number value1, Number key2, Number value2) Constructs a map given alternating keys and values. This is mostly useful as an optimization forwith(Number, Number, Number...)when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number keys and values to primitive long and long, regardless of which Number type was used.- Parameters:
key0- a Number key; will be converted to primitive longvalue0- a Number for a value; will be converted to primitive longkey1- a Number key; will be converted to primitive longvalue1- a Number for a value; will be converted to primitive longkey2- a Number key; will be converted to primitive longvalue2- a Number for a value; will be converted to primitive long- Returns:
- a new map containing the given key-value pairs
-
with
public static LongLongMap with(Number key0, Number value0, Number key1, Number value1, Number key2, Number value2, Number key3, Number value3) Constructs a map given alternating keys and values. This is mostly useful as an optimization forwith(Number, Number, Number...)when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number keys and values to primitive long and long, regardless of which Number type was used.- Parameters:
key0- a Number key; will be converted to primitive longvalue0- a Number for a value; will be converted to primitive longkey1- a Number key; will be converted to primitive longvalue1- a Number for a value; will be converted to primitive longkey2- a Number key; will be converted to primitive longvalue2- a Number for a value; will be converted to primitive longkey3- a Number key; will be converted to primitive longvalue3- a Number for a value; will be converted to primitive long- Returns:
- a new map containing the given key-value pairs
-
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 useLongLongMap(long[], long[]), which takes all keys and then all values. This needs all keys to be some kind of (boxed) Number, and converts them to primitivelongs. It also needs all values to be a (boxed) Number, and converts them to primitivelongs. Any keys or values that aren'tNumbers have that entry skipped.- Parameters:
key0- the first key; will be converted to a primitive longvalue0- the first value; will be converted to a primitive longrest- an array or varargs of Number elements- Returns:
- a new map containing the given key-value pairs
-
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. All keys and values must be some type of boxed Number, such asIntegerorDouble, and will be converted to primitivelongs.
If any item inpairscannot be cast to the appropriate 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. Ifpairsis a Number 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 Number 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). Each item can vary significantly in length, and should useBase.BASE10digits, which should be human-readable. 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 BASE10 chars
-
putLegible
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). Each item can vary significantly in length, and should useBase.BASE10digits, which should be human-readable. 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 BASE10 charsentrySeparator- the String separating every key-value pair
-
putLegible
Adds items to this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, String, boolean, LongAppender, LongAppender). Each item can vary significantly in length, and should useBase.BASE10digits, which should be human-readable. 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 BASE10 charsentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding value
-
putLegible
public void putLegible(String str, String entrySeparator, String keyValueSeparator, int offset, int length) Puts key-value pairs into this map drawn from the result oftoString(String)orappendTo(StringBuilder, String, String, boolean, LongAppender, LongAppender). Each item can vary significantly in length, and should useBase.BASE10digits, which should be human-readable. 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 BASE10 charsentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valueoffset- the first position to read BASE10 chars from instrlength- how many chars to read; -1 is treated as maximum length
-
putPairsPrimitive
public void putPairsPrimitive(long... pairs) 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. All keys and values must be primitivelongs.
Ifpairsis a long 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 long elements
-
withPrimitive
Constructs an empty map. 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.- Returns:
- a new map containing nothing
-
withPrimitive
Constructs a single-entry map given one key and one value. This is mostly useful as an optimization forwith(Number, Number, Number...)when there's no "rest" of the keys or values. Unlike the vararg with(), this doesn't box its arguments into Number items.- Parameters:
key0- the first and only keyvalue0- the first and only value- Returns:
- a new map containing just the entry mapping key0 to value0
-
withPrimitive
Constructs a map given alternating keys and values. This is mostly useful as an optimization forwith(Number, Number, Number...)when there's no "rest" of the keys or values. Unlike the vararg with(), this doesn't box its arguments into Number items.- Parameters:
key0- a long keyvalue0- a long valuekey1- a long keyvalue1- a long value- Returns:
- a new map containing the given key-value pairs
-
withPrimitive
public static LongLongMap withPrimitive(long key0, long value0, long key1, long value1, long key2, long value2) Constructs a map given alternating keys and values. This is mostly useful as an optimization forwith(Number, Number, Number...)when there's no "rest" of the keys or values. Unlike the vararg with(), this doesn't box its arguments into Number items.- Parameters:
key0- a long keyvalue0- a long valuekey1- a long keyvalue1- a long valuekey2- a long keyvalue2- a long value- Returns:
- a new map containing the given key-value pairs
-
withPrimitive
public static LongLongMap withPrimitive(long key0, long value0, long key1, long value1, long key2, long value2, long key3, long value3) Constructs a map given alternating keys and values. This is mostly useful as an optimization forwith(Number, Number, Number...)when there's no "rest" of the keys or values. Unlike the vararg with(), this doesn't box its arguments into Number items.- Parameters:
key0- a long keyvalue0- a long valuekey1- a long keyvalue1- a long valuekey2- a long keyvalue2- a long valuekey3- a long keyvalue3- a long value- Returns:
- a new map containing the given key-value pairs
-
withPrimitive
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 useLongLongMap(long[], long[]), which takes all keys and then all values. This needs all keys and all values to be primitivelongs; if any are boxed, then you should callwith(Number, Number, Number...).
This method has to be named differently fromwith(Number, Number, Number...)to disambiguate the two, which would otherwise both be callable with all primitives (due to auto-boxing).- Parameters:
key0- the first key; must not be boxedvalue0- the first value; must not be boxedrest- an array or varargs of primitive long elements- Returns:
- a new map containing the given keys and values
-
parse
Creates a new map by parsing all ofstr, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".- Parameters:
str- a String containing parseable textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding value
-
parse
public static LongLongMap parse(String str, String entrySeparator, String keyValueSeparator, boolean brackets) Creates a new map by parsing all ofstr(or ifbracketsis true, all but the first and last chars), with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".- Parameters:
str- a String containing parseable textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valuebrackets- if true, the first and last chars instrwill be ignored
-
parse
public static LongLongMap parse(String str, String entrySeparator, String keyValueSeparator, int offset, int length) Creates a new map by parsing the given subrange ofstr, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".- Parameters:
str- a String containing parseable textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valueoffset- the first position to read parseable text from instrlength- how many chars to read; -1 is treated as maximum length
-