Package com.github.tommyettinger.ds
Class EnumMap<V>
java.lang.Object
com.github.tommyettinger.ds.EnumMap<V>
- Direct Known Subclasses:
EnumOrderedMap
An unordered map where the keys are
This class never actually hashes keys in its primary operations (get(), put(), remove(), containsKey(), etc.), since it can rely on keys having an Enum type, and so having
The key universe is an important concept here; it is simply an array of all possible Enum values the EnumMap can use as keys, in the specific order they are declared. You almost always get a key universe by calling
This class tries to be as compatible as possible with
Enums and values are objects. Null keys are not allowed; null values are permitted.
Unlike EnumMap, this does not require a Class at construction time, which can be useful for serialization
purposes. No allocation is done unless this is changing its table size and/or key universe.
This class never actually hashes keys in its primary operations (get(), put(), remove(), containsKey(), etc.), since it can rely on keys having an Enum type, and so having
Enum.ordinal() available. The ordinal allows constant-time access
to a guaranteed-unique int that will always be non-negative and less than the size of the key universe. The table of
possible values always starts sized to fit exactly as many values as there are keys in the key universe.
The key universe is an important concept here; it is simply an array of all possible Enum values the EnumMap can use as keys, in the specific order they are declared. You almost always get a key universe by calling
MyEnum.values(), but you
can also use Class.getEnumConstants() for an Enum class. You can and generally should reuse key universes in order to
avoid allocations and/or save memory; the constructor EnumMap(Enum[]) (with no values given) creates an empty EnumMap with
a given key universe. If you need to use the zero-argument constructor, you can, and the key universe will be obtained from the
first key placed into the EnumMap. You can also set the key universe with clearToUniverse(Enum[]), in the process of
clearing the map.
This class tries to be as compatible as possible with
EnumMap, though this expands on that where possible.-
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 EnumMap.Entries<V>protected EnumMap.Entries<V>protected EnumMap.Keysprotected EnumMap.Keysprotected intprotected Enum<?>[]protected EnumMap.Values<V>protected EnumMap.Values<V>protected Object[] -
Constructor Summary
ConstructorsConstructorDescriptionEnumMap()Empty constructor; using this will postpone creating the key universe and allocating the value table untilput(java.lang.Enum<?>, V)is first called (potentially indirectly).Creates a new map identical to the specified EnumMap.Initializes this map so that it has exactly enough capacity as needed to contain each Enum constant defined by the ClassuniverseClass, assuming universeClass is non-null.Initializes this map so that it has exactly enough capacity as needed to contain each Enum constant defined inuniverse, assuming universe stores every possible constant in one Enum type.Given two side-by-side arrays, one of Enum keys, one of V values, this constructs a map and inserts each pair of key and value into it.EnumMap(Collection<? extends Enum<?>> keys, Collection<? extends V> values) Given two side-by-side collections, one of Enum keys, one of V values, this constructs a map and inserts each pair of key and value into it.Creates a new map identical to the specified map. -
Method Summary
Modifier and TypeMethodDescriptionappendTo(StringBuilder sb, String entrySeparator, boolean braces) appendTo(StringBuilder sb, String entrySeparator, String keyValueSeparator, boolean braces, Appender<Enum<?>> keyAppender, Appender<V> valueAppender) voidclear()Removes all the elements from this map.voidclearToUniverse(Class<? extends Enum<?>> universe) Removes all the elements from this map and can reset the universe of possible Enum items this can hold.voidclearToUniverse(Enum<?>[] universe) Removes all the elements from this map and can reset the universe of possible Enum items this can hold.combine(Enum<?> 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)).voidcombine(Map<? extends Enum<?>, ? extends V> other, com.github.tommyettinger.function.ObjObjToObjBiFunction<? super V, ? super V, ? extends V> remappingFunction) Simply callscombine(Enum, Object, ObjObjToObjBiFunction)on this map using every key-value pair inother.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.entrySet()Returns a Set of Map.Entry, containing the entries in the map.booleanbooleanequalsIdentity(Object obj) Uses == for comparison of each value.Enum<?>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.getOrDefault(Object key, V defaultValue) Returns the value for the specified key, or the given default value if the key is not in the map.Enum<?>[]Gets the current key universe; this is a technically-mutable array, but should never be modified.inthashCode()protected ObjectIf the given Object isnull, this replaces it with a placeholder value (Utilities.neverIdentical); otherwise, it returns the given Object as-is.booleanisEmpty()Returns true if the map is empty.EnumMap.MapIterator<V,Map.Entry<Enum<?>, V>> iterator()Reuses the iterator of the reusedEnumMap.Entriesproduced byentrySet(); does not permit nested iteration.keySet()Returns aSetview of the keys contained in this map.static <V> EnumMap<V>Constructs an empty map that can store keys from the given universe, using the specified generic type for values.booleannotEmpty()Returns true if the map has one or more items.static <V> EnumMap<V>of()Constructs an empty map given the types as generic type arguments; an alias forwith().static <V> EnumMap<V>Constructs a single-entry map given one key and one value; an alias forwith(Enum, Object).static <V> EnumMap<V>Constructs a map given alternating keys and values; an alias forwith(Enum, Object, Object...).static <V> EnumMap<V>parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<Enum<?>> 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 <V> EnumMap<V>parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<Enum<?>> 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 <V> EnumMap<V>parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<Enum<?>> 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"=".Returns the old value associated with the specified key, or this map'sdefaultValueif there was no prior value.voidPuts 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.voidGiven two side-by-side arrays, one of keys, one of values, this inserts each pair of key and value into this map withput(Enum, Object).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().voidputAll(Collection<? extends Enum<?>> keys, Collection<? extends V> values) Given two side-by-side collections, one of Enum keys, one of V 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).voidputLegible(String str, PartialParser<Enum<?>> 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<Enum<?>> 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<Enum<?>> 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<Enum<?>> 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(Enum<?> key, V value, V defaultValue) Acts likeput(Enum, Object), but uses the specifieddefaultValueinstead ofthe default value for this EnumMap.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 VIf the given Object isUtilities.neverIdentical, this "releases its hold" on that placeholder value and returns null; otherwise, it returns the given Object (cast to V if non-null).voidsetDefaultValue(V defaultValue) Sets the default value, aVwhich is returned byget(Object)if the key is not found.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<Enum<?>> 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 <V> EnumMap<V>with()Constructs an empty map given the types as generic type arguments.static <V> EnumMap<V>Constructs a single-entry map given one key and one value.static <V> EnumMap<V>Constructs a single-entry map given two key-value pairs.static <V> EnumMap<V>Constructs a single-entry map given three key-value pairs.static <V> EnumMap<V>with(Enum<?> key0, V value0, Enum<?> key1, V value1, Enum<?> key2, V value2, Enum<?> key3, V value3) Constructs a single-entry map given four key-value pairs.static <V> EnumMap<V>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, spliteratorMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, merge, putIfAbsent, remove, replace, replaceAll
-
Field Details
-
size
protected int size -
universe
-
valueTable
-
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. Defaults tonull.
-
-
Constructor Details
-
EnumMap
public EnumMap()Empty constructor; using this will postpone creating the key universe and allocating the value table untilput(java.lang.Enum<?>, V)is first called (potentially indirectly). You can also useclearToUniverse(java.lang.Enum<?>[])to set the key universe and value table. -
EnumMap
Initializes this map so that it has exactly enough capacity as needed to contain each Enum constant defined inuniverse, assuming universe stores every possible constant in one Enum type. This map will start empty. You almost always obtain universe from callingvalues()on an Enum type, and you can share one reference to one Enum array across many EnumMap instances if you don't modify the shared array. Sharing the same universe helps save some memory if you have (very) many EnumMap instances.- Parameters:
universe- almost always, the result of callingvalues()on an Enum type; used directly, not copied
-
EnumMap
Initializes this map so that it has exactly enough capacity as needed to contain each Enum constant defined by the ClassuniverseClass, assuming universeClass is non-null. This simply callsEnumMap(Enum[])for convenience. Note that this constructor allocates a new array of Enum constants each time it is called, where if you useEnumMap(Enum[]), you can reuse an unmodified array to reduce allocations.- Parameters:
universeClass- the Class of an Enum type that defines the universe of valid Enum items this can hold
-
EnumMap
Creates a new map identical to the specified EnumMap. This will share a key universe with the given EnumMap, if non-null.- Parameters:
map- an EnumMap to copy
-
EnumMap
Creates a new map identical to the specified map.- Parameters:
map- a Map to copy; EnumMap or its subclasses will be faster
-
EnumMap
Given two side-by-side arrays, one of Enum keys, one of V 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 Enum keysvalues- an array of V values
-
EnumMap
Given two side-by-side collections, one of Enum keys, one of V 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 Enum keysvalues- a Collection of V values
-
-
Method Details
-
hold
If the given Object isnull, this replaces it with a placeholder value (Utilities.neverIdentical); otherwise, it returns the given Object as-is.- Parameters:
o- any Object; will be returned as-is unless it is null- Returns:
- the given Object or
Utilities.neverIdentical
-
release
If the given Object isUtilities.neverIdentical, this "releases its hold" on that placeholder value and returns null; otherwise, it returns the given Object (cast to V if non-null).- Parameters:
o- any Object, but should be the placeholderUtilities.neverIdenticalor a V instance- Returns:
- the V passed in, or null if it is the placeholder
-
put
Returns the old value associated with the specified key, or this map'sdefaultValueif there was no prior value. If this EnumMap does not yet have a key universe and/or value table, this gets the key universe fromkeyand uses it from now on for this EnumMap.- Specified by:
putin interfaceMap<Enum<?>,V> - Parameters:
key- the Enum key to try to place into this EnumMapvalue- the V value to associate withkey- Returns:
- the previous value associated with
key, orgetDefaultValue()if the given key was not present
-
putOrDefault
Acts likeput(Enum, Object), but uses the specifieddefaultValueinstead ofthe default value for this EnumMap.- Parameters:
key- the Enum key to try to place into this EnumMapvalue- the V value to associate withkeydefaultValue- the V value to return ifkeywas not already present- Returns:
- the previous value associated with
key, or the givendefaultValueif the given key was not present
-
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. If this EnumMap doesn't yet have a key universe, it will now share a key universe with the givenmap. Even if the given EnumMap is empty, it can still be used to obtain a key universe for this EnumMap (assuming it has a key universe).- Parameters:
map- a map with compatible key and value types; will not be modified
-
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(). Delegates toputAll(Enum[], int, Object[], int, int).- 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(). Delegates toputAll(Enum[], int, Object[], int, int).- 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 withput(Enum, Object).- 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
-
putAll
Given two side-by-side collections, one of Enum keys, one of V values, this inserts each pair of key and value into this map with put().- Parameters:
keys- a Collection of Enum keysvalues- a Collection of V values
-
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<Enum<?>,V>
-
remove
-
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(EnumMap)is more specific and can be more efficient by using the internal details of this class.- Specified by:
putAllin interfaceMap<Enum<?>,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
-
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
-
clear
public void clear()Removes all the elements from this map. The map will be empty after this call returns. This does not change the universe of possible Enum items this can hold. -
clearToUniverse
Removes all the elements from this map and can reset the universe of possible Enum items this can hold. The map will be empty after this call returns. This changes the universe of possible Enum items this can hold to matchuniverse. Ifuniverseis null, this resets this map to the state it would have afterEnumMap()was called. If the table this would need is the same size as or smaller than the current table (such as ifuniverseis the same as the universe here), this will not allocate, but will still clear any items this holds and will set the universe to the given one. Otherwise, this allocates and uses a new table of a larger size, with nothing in it, and uses the given universe. This always usesuniversedirectly, without copying.
This can be useful to allow an EnumMap that was created withEnumMap()to share a universe with other EnumMaps.- Parameters:
universe- the universe of possible Enum items this can hold; almost always produced byvalues()on an Enum
-
clearToUniverse
Removes all the elements from this map and can reset the universe of possible Enum items this can hold. The map will be empty after this call returns. This changes the universe of possible Enum items this can hold to match the Enum constants inuniverse. Ifuniverseis null, this resets this map to the state it would have afterEnumMap()was called. If the table this would need is the same size as or smaller than the current table (such as ifuniverseis the same as the universe here), this will not allocate, but will still clear any items this holds and will set the universe to the given one. Otherwise, this allocates and uses a new table of a larger size, with nothing in it, and uses the given universe. This callsClass.getEnumConstants()if universe is non-null, which allocates a new array.
You may want to prefer callingclearToUniverse(Enum[])(the overload that takes an array), because it can be used to share one universe array between many EnumMap instances. This overload, given a Class, has to callClass.getEnumConstants()and thus allocate a new array each time this is called.- Parameters:
universe- the Class of an Enum type that stores the universe of possible Enum items this can hold
-
getUniverse
Gets the current key universe; this is a technically-mutable array, but should never be modified. To set the universe on an existing EnumMap (with existing contents), you can useclearToUniverse(Enum[]). If an EnumMap has not been initialized, just adding a key will set the key universe to match the given item.- Returns:
- the current key universe
-
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
-
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<Enum<?>,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<Enum<?>,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 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:
identity- If true, uses == to compare the specified value with values in the map. If false, usesequals(Object).- Returns:
- the corresponding Enum if the value was found, or null otherwise
-
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<Enum<?>> keyAppender, Appender<V> valueAppender) Makes a String from the contents of this EnumMap, 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 toString representation, you can useAppender::appendas an appender, or to use the readable EnumEnum.name(), useAppender.ENUM_NAME_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 an Enum, 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<Enum<?>> keyAppender, Appender<V> valueAppender) Appends to a StringBuilder from the contents of this EnumMap, 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 an Enum, 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
-
replace
-
combine
public V combine(Enum<?> 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 Enum<?>, ? extends V> other, com.github.tommyettinger.function.ObjObjToObjBiFunction<? super V, ? super V, ? extends V> remappingFunction) Simply callscombine(Enum, 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 reusedEnumMap.Entriesproduced byentrySet(); does not permit nested iteration. Iterate overEntries(EnumMap)if you need nested or multithreaded iteration. You can remove an Entry from this EnumMap 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
EnumMap.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 theEnumMap.Valuesconstructor for nested or multithreaded iteration.- Specified by:
valuesin interfaceMap<Enum<?>,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 theEnumMap.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:
V- 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(Enum, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
V- 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(Enum, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
V- the type of value0- Parameters:
key0- an Enum keyvalue0- a V valuekey1- an Enum keyvalue1- a V value- Returns:
- a new map containing entries mapping each key to the following value
-
with
public static <V> EnumMap<V> with(Enum<?> key0, V value0, Enum<?> key1, V value1, Enum<?> key2, V value2) Constructs a single-entry map given three key-value pairs. This is mostly useful as an optimization forwith(Enum, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
V- the type of value0- Parameters:
key0- an Enum keyvalue0- a V valuekey1- an Enum keyvalue1- a V valuekey2- an Enum keyvalue2- a V value- Returns:
- a new map containing entries mapping each key to the following value
-
with
public static <V> EnumMap<V> with(Enum<?> key0, V value0, Enum<?> key1, V value1, Enum<?> key2, V value2, Enum<?> key3, V value3) Constructs a single-entry map given four key-value pairs. This is mostly useful as an optimization forwith(Enum, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
V- the type of value0- Parameters:
key0- an Enum keyvalue0- a V valuekey1- an Enum keyvalue1- a V valuekey2- an Enum keyvalue2- a V valuekey3- an Enum 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 useEnumMap(Enum[], 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 Enum as their type or values that don't have V as their type have that entry skipped.- Type Parameters:
V- the type of values, inferred from value0- Parameters:
key0- the first key (an Enum)value0- the first value; will be used to determine the type of all valuesrest- an array or varargs of alternating Enum, V, Enum, 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. Usually, keyParser is produced byPartialParser.enumParser(ObjToObjFunction). 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 aEnum<?>key from a section ofstr, typically produced byPartialParser.enumParser(ObjToObjFunction)valueParser- a PartialParser that returns aVvalue from a section ofstr
-
putLegible
public void putLegible(String str, String entrySeparator, PartialParser<Enum<?>> 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. Usually, keyParser is produced byPartialParser.enumParser(ObjToObjFunction). 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 aEnum<?>key from a section ofstr, typically produced byPartialParser.enumParser(ObjToObjFunction)valueParser- a PartialParser that returns aVvalue from a section ofstr
-
putLegible
public void putLegible(String str, String entrySeparator, String keyValueSeparator, PartialParser<Enum<?>> 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. Usually, keyParser is produced byPartialParser.enumParser(ObjToObjFunction). 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 aEnum<?>key from a section ofstr, typically produced byPartialParser.enumParser(ObjToObjFunction)valueParser- a PartialParser that returns aVvalue from a section ofstr
-
putLegible
public void putLegible(String str, String entrySeparator, String keyValueSeparator, PartialParser<Enum<?>> 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. Usually, keyParser is produced byPartialParser.enumParser(ObjToObjFunction). 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 aEnum<?>key from a section ofstr, typically produced byPartialParser.enumParser(ObjToObjFunction)valueParser- 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 <V> EnumMap<V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<Enum<?>> 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). ThekeyParseris often produced byPartialParser.enumParser(ObjToObjFunction).- 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 anEnumkey from a section ofstrvalueParser- a PartialParser that returns aVvalue from a section ofstr
-
parse
public static <V> EnumMap<V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<Enum<?>> 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). ThekeyParseris often produced byPartialParser.enumParser(ObjToObjFunction).- 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 anEnumkey 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 <V> EnumMap<V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<Enum<?>> 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). ThekeyParseris often produced byPartialParser.enumParser(ObjToObjFunction).- 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 anEnumkey 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
-
of
Constructs an empty map given the types as generic type arguments; an alias forwith().- Type Parameters:
V- the type of values- Returns:
- a new map containing nothing
-
of
Constructs a single-entry map given one key and one value; an alias forwith(Enum, Object).- Type Parameters:
V- 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
-
of
Constructs a map given alternating keys and values; an alias forwith(Enum, Object, Object...).- Type Parameters:
V- the type of values, inferred from value0- Parameters:
key0- the first key (an Enum)value0- the first value; will be used to determine the type of all valuesrest- an array or varargs of alternating Enum, V, Enum, V... elements- Returns:
- a new map containing the given keys and values
-
noneOf
Constructs an empty map that can store keys from the given universe, using the specified generic type for values. The universe is usually obtained from an Enum type'svalues()method, and is often shared between Enum-keyed maps and sets.- Type Parameters:
V- the type of values- Parameters:
universe- a key universe, as an array of Enum constants typically obtained viavalues()- Returns:
- a new map containing nothing
-