Package com.github.tommyettinger.ds
Class CaseInsensitiveOrderedMap<V>
java.lang.Object
com.github.tommyettinger.ds.ObjectObjectMap<K,V>
com.github.tommyettinger.ds.ObjectObjectOrderedMap<CharSequence,V>
com.github.tommyettinger.ds.CaseInsensitiveOrderedMap<V>
- All Implemented Interfaces:
Arrangeable,Ordered<CharSequence>,Iterable<Map.Entry<CharSequence,,V>> Map<CharSequence,V>
A custom variant on ObjectObjectOrderedMap that always uses CharSequence keys and compares them as case-insensitive.
This uses a fairly complex, quite-optimized hashing function because it needs to hash CharSequences rather
often, and to do so ignoring case means
This is very similar to
This is also very similar to
String.hashCode() won't work, plus not all CharSequences
implement hashCode() themselves (such as StringBuilder). User code similar to this can often get away
with a simple polynomial hash (the typical Java kind, used by String and Arrays), or if more speed is needed,
one with some of these
optimizations by Richard Startin. If you don't want to write or benchmark a hash function (which is quite
understandable), Utilities.hashCodeIgnoreCase(CharSequence) can get a case-insensitive hash of any
CharSequence, as a long. It does this without allocating new Strings all over, where many case-insensitive
algorithms do allocate quite a lot, but it does this by handling case incorrectly for the Georgian alphabet.
If I see Georgian text in-the-wild, I may reconsider, but I don't think that particular alphabet is in
widespread use. There's also Utilities.equalsIgnoreCase(CharSequence, CharSequence) for equality
comparisons that are similarly case-insensitive, except for Georgian.
This is very similar to
CaseInsensitiveMap, except that this class maintains insertion order and can be sorted with
ObjectObjectOrderedMap.sort(), ObjectObjectOrderedMap.sortByValue(Comparator), etc. Note that because each CharSequence is stored in here
in its original form (not modified to make it ignore case), the sorted order might be different than you expect.
Utilities.compareIgnoreCase(CharSequence, CharSequence) can be used to sort this as case-insensitive.
This is also very similar to
FilteredStringOrderedMap when its editor
is Character.toUpperCase(char) or Casing.caseUp(char).
FilteredStringOrderedMap works with Strings rather than CharSequences, which
may be more convenient, and allows filtering some characters out of hashing and equality comparisons. If you want a
case-insensitive map that ignores any non-letter characters in a String, then CaseInsensitiveOrderedMap won't do,
but new FilteredStringOrderedMap<>(CharPredicates.IS_LETTER, Casing::caseUp will work. Note that GWT only
handles Character.isLetter(char) for ASCII letters; CharPredicates in this library provides cross-platform
predicates that use CharBitSet to store their data, and the library RegExodus offers replacements in
Category for other Unicode categories, such as upper-case letters, currency symbols, decimal digits, and so on.-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class com.github.tommyettinger.ds.ObjectObjectOrderedMap
ObjectObjectOrderedMap.OrderedMapEntries<K,V>, ObjectObjectOrderedMap.OrderedMapKeys<K, V>, ObjectObjectOrderedMap.OrderedMapValues<K, V> Nested classes/interfaces inherited from class com.github.tommyettinger.ds.ObjectObjectMap
ObjectObjectMap.Entries<K,V>, ObjectObjectMap.Entry<K, V>, ObjectObjectMap.Keys<K, V>, ObjectObjectMap.MapIterator<K, V, I>, ObjectObjectMap.Values<K, V> Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.Arrangeable
Arrangeable.ArrangeableList<T>Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.Ordered
Ordered.OfBoolean, Ordered.OfByte, Ordered.OfChar, Ordered.OfDouble, Ordered.OfFloat, Ordered.OfInt, Ordered.OfLong, Ordered.OfShort -
Field Summary
Fields inherited from class com.github.tommyettinger.ds.ObjectObjectOrderedMap
keysFields inherited from class com.github.tommyettinger.ds.ObjectObjectMap
defaultValue, entries1, entries2, hashMultiplier, keys1, keys2, keyTable, loadFactor, mask, shift, size, threshold, values1, values2, valueTable -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor().CaseInsensitiveOrderedMap(int initialCapacity) Creates a new map with the specified initial capacity and a load factor ofUtilities.getDefaultLoadFactor().CaseInsensitiveOrderedMap(int initialCapacity, float loadFactor) Creates a new map with the specified initial capacity and load factor.CaseInsensitiveOrderedMap(int initialCapacity, float loadFactor, OrderType ordering) Creates a new map with the specified initial capacity and load factor.CaseInsensitiveOrderedMap(int initialCapacity, OrderType ordering) Creates a new map with the given starting capacity and a load factor ofUtilities.getDefaultLoadFactor().CaseInsensitiveOrderedMap(CaseInsensitiveOrderedMap<? extends V> map) Creates a new map identical to the specified map.CaseInsensitiveOrderedMap(ObjectObjectMap<? extends CharSequence, ? extends V> map, OrderType ordering) Creates a new map identical to the specified map.CaseInsensitiveOrderedMap(ObjectObjectOrderedMap<? extends CharSequence, ? extends V> other, int offset, int count) Creates a new set by copyingcountitems from the given ObjectObjectOrderedMap (or a subclass, such as CaseInsensitiveOrderedMap), starting atoffsetin that Map, into this.CaseInsensitiveOrderedMap(ObjectObjectOrderedMap<? extends CharSequence, ? extends V> other, int offset, int count, OrderType ordering) Creates a new set by copyingcountitems from the given ObjectObjectOrderedMap, starting atoffsetin that Map, into this.CaseInsensitiveOrderedMap(OrderType ordering) Creates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor().CaseInsensitiveOrderedMap(CharSequence[] 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.CaseInsensitiveOrderedMap(CharSequence[] keys, V[] values, OrderType ordering) 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.CaseInsensitiveOrderedMap(Collection<? extends CharSequence> 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.CaseInsensitiveOrderedMap(Collection<? extends CharSequence> keys, Collection<? extends V> values, OrderType ordering) 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.CaseInsensitiveOrderedMap(Map<? extends CharSequence, ? extends V> map) Creates a new map identical to the specified map.CaseInsensitiveOrderedMap(Map<? extends CharSequence, ? extends V> map, OrderType ordering) Creates a new map identical to the specified map. -
Method Summary
Modifier and TypeMethodDescriptionbooleanprotected booleanCompares the objects left and right, which are usually keys, for equality, returning true if they are considered equal.inthashCode()keySet()Returns aSetview of the keys contained in this map.static <V> CaseInsensitiveOrderedMap<V>parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<V> valueParser) Creates a new map by parsing all ofstrwith the given PartialParser for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".static <V> CaseInsensitiveOrderedMap<V>parse(String str, String entrySeparator, String keyValueSeparator, 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 values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".static <V> CaseInsensitiveOrderedMap<V>parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<V> valueParser, int offset, int length) Creates a new map by parsing the given subrange ofstrwith the given PartialParser for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".protected intReturns an index >= 0 and <=ObjectObjectMap.maskfor the specifieditem, mixed.static <V> CaseInsensitiveOrderedMap<V>with(CharSequence key0, V value0) Constructs a single-entry map given one key and one value.static <V> CaseInsensitiveOrderedMap<V>with(CharSequence key0, V value0, CharSequence key1, V value1) Constructs a single-entry map given two key-value pairs.static <V> CaseInsensitiveOrderedMap<V>with(CharSequence key0, V value0, CharSequence key1, V value1, CharSequence key2, V value2) Constructs a single-entry map given three key-value pairs.static <V> CaseInsensitiveOrderedMap<V>with(CharSequence key0, V value0, CharSequence key1, V value1, CharSequence key2, V value2, CharSequence key3, V value3) Constructs a single-entry map given four key-value pairs.static <V> CaseInsensitiveOrderedMap<V>with(CharSequence key0, V value0, Object... rest) Constructs a map given alternating keys and values.Methods inherited from class com.github.tommyettinger.ds.ObjectObjectOrderedMap
alter, alterAt, appendTo, clear, clear, ensureCapacity, entrySet, getAt, iterator, keyAt, order, parse, parse, parse, put, put, putAll, putAll, putAll, putOrDefault, remove, removeAt, removeRange, setAt, sort, sort, sortByValue, truncate, values, with, with, with, with, with, withMethods inherited from class com.github.tommyettinger.ds.ObjectObjectMap
appendTo, combine, combine, containsKey, containsValue, containsValue, equalsIdentity, findKey, findKey, get, getDefaultValue, getHashMultiplier, getLoadFactor, getOrDefault, getTableSize, isEmpty, locateKey, notEmpty, putAll, putAll, putAll, putAll, putAll, putAll, putLegible, putLegible, putLegible, putLegible, putPairs, putResize, replace, resize, setDefaultValue, setHashMultiplier, setLoadFactor, shrink, size, toString, toString, toString, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.github.tommyettinger.ds.Arrangeable
rearrange, shuffle, sizeMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, merge, putIfAbsent, remove, replace, replaceAllMethods inherited from interface com.github.tommyettinger.ds.Ordered
getOrderType, random, random, reverse, selectRanked, selectRankedIndex, shuffle, swap
-
Constructor Details
-
CaseInsensitiveOrderedMap
Creates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor().- Parameters:
ordering- determines what implementationObjectObjectOrderedMap.order()will use
-
CaseInsensitiveOrderedMap
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.ordering- determines what implementationObjectObjectOrderedMap.order()will use
-
CaseInsensitiveOrderedMap
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 <= 1ordering- determines what implementationObjectObjectOrderedMap.order()will use
-
CaseInsensitiveOrderedMap
Creates a new map identical to the specified map.- Parameters:
map- the map to copyordering- determines what implementationObjectObjectOrderedMap.order()will use
-
CaseInsensitiveOrderedMap
public CaseInsensitiveOrderedMap(ObjectObjectMap<? extends CharSequence, ? extends V> map, OrderType ordering) Creates a new map identical to the specified map.- Parameters:
map- the map to copyordering- determines what implementationObjectObjectOrderedMap.order()will use
-
CaseInsensitiveOrderedMap
public CaseInsensitiveOrderedMap(ObjectObjectOrderedMap<? extends CharSequence, ? extends V> other, int offset, int count, OrderType ordering) Creates a new set by copyingcountitems from the given ObjectObjectOrderedMap, starting atoffsetin that Map, into this.- Parameters:
other- another ObjectObjectOrderedMap of the same typeoffset- the first index in other's ordering to draw an item fromcount- how many items to copy from otherordering- determines what implementationObjectObjectOrderedMap.order()will use
-
CaseInsensitiveOrderedMap
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 valuesordering- determines what implementationObjectObjectOrderedMap.order()will use
-
CaseInsensitiveOrderedMap
public CaseInsensitiveOrderedMap(Collection<? extends CharSequence> keys, Collection<? extends V> values, OrderType ordering) 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 valuesordering- determines what implementationObjectObjectOrderedMap.order()will use
-
CaseInsensitiveOrderedMap
public CaseInsensitiveOrderedMap()Creates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor(). -
CaseInsensitiveOrderedMap
public CaseInsensitiveOrderedMap(int initialCapacity) Creates a new map with the specified initial capacity and a load factor ofUtilities.getDefaultLoadFactor(). 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.
-
CaseInsensitiveOrderedMap
public CaseInsensitiveOrderedMap(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
-
CaseInsensitiveOrderedMap
Creates a new map identical to the specified map.- Parameters:
map- a Map to copy; ObjectObjectOrderedMap and subclasses of it will be faster
-
CaseInsensitiveOrderedMap
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
-
CaseInsensitiveOrderedMap
Creates a new map identical to the specified map.- Parameters:
map- a CaseInsensitiveOrderedMap to copy
-
CaseInsensitiveOrderedMap
public CaseInsensitiveOrderedMap(Collection<? extends CharSequence> 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. 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
-
CaseInsensitiveOrderedMap
public CaseInsensitiveOrderedMap(ObjectObjectOrderedMap<? extends CharSequence, ? extends V> other, int offset, int count) Creates a new set by copyingcountitems from the given ObjectObjectOrderedMap (or a subclass, such as CaseInsensitiveOrderedMap), starting atoffsetin that Map, into this.- Parameters:
other- another ObjectObjectOrderedMap of the same types (key must be some kind of CharSequence)offset- the first index in other's ordering to draw an item fromcount- how many items to copy from other
-
-
Method Details
-
place
Description copied from class:ObjectObjectMapReturns an index >= 0 and <=ObjectObjectMap.maskfor the specifieditem, mixed.- Overrides:
placein classObjectObjectMap<CharSequence,V> - Parameters:
item- a non-null Object; its hashCode() method should be used by most implementations- Returns:
- an index between 0 and
ObjectObjectMap.mask(both inclusive)
-
equate
Description copied from class:ObjectObjectMapCompares 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,ObjectObjectMap.place(Object)is also overridden if this method is.- Overrides:
equatein classObjectObjectMap<CharSequence,V> - 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
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceMap<CharSequence,V> - Overrides:
hashCodein classObjectObjectMap<CharSequence,V>
-
equals
- Specified by:
equalsin interfaceMap<CharSequence,V> - Overrides:
equalsin classObjectObjectMap<CharSequence,V>
-
keySet
Description copied from class:ObjectObjectOrderedMapReturns 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
OrderedMapKeys(ObjectObjectOrderedMap)constructor for nested or multithreaded iteration.- Specified by:
keySetin interfaceMap<CharSequence,V> - Overrides:
keySetin classObjectObjectOrderedMap<CharSequence,V> - Returns:
- a set view of the keys contained in this map
-
with
Constructs a single-entry map given one key and one value. This is mostly useful as an optimization forObjectObjectOrderedMap.with(Object, 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
public static <V> CaseInsensitiveOrderedMap<V> with(CharSequence key0, V value0, CharSequence key1, V value1) Constructs a single-entry map given two key-value pairs. This is mostly useful as an optimization forObjectObjectOrderedMap.with(Object, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
V- 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 <V> CaseInsensitiveOrderedMap<V> with(CharSequence key0, V value0, CharSequence key1, V value1, CharSequence key2, V value2) Constructs a single-entry map given three key-value pairs. This is mostly useful as an optimization forObjectObjectOrderedMap.with(Object, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
V- the type of value0- Parameters:
key0- a CharSequence keyvalue0- a V valuekey1- a CharSequence keyvalue1- a V valuekey2- a CharSequence keyvalue2- a V value- Returns:
- a new map containing entries mapping each key to the following value
-
with
public static <V> CaseInsensitiveOrderedMap<V> with(CharSequence key0, V value0, CharSequence key1, V value1, CharSequence key2, V value2, CharSequence key3, V value3) Constructs a single-entry map given four key-value pairs. This is mostly useful as an optimization forObjectObjectOrderedMap.with(Object, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
V- the type of value0- Parameters:
key0- a CharSequence keyvalue0- a V valuekey1- a CharSequence keyvalue1- a V valuekey2- a CharSequence keyvalue2- a V valuekey3- a CharSequence 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 useCaseInsensitiveOrderedMap(CharSequence[], 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 CharSequence 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; will be used to determine the type of all keysvalue0- the first value; will be used to determine the type of all valuesrest- an array or varargs of alternating CharSequence, V, CharSequence, V... elements- Returns:
- a new map containing the given keys and values
-
parse
public static <V> CaseInsensitiveOrderedMap<V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<V> valueParser) Creates a new map by parsing all ofstrwith the given PartialParser 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 valuevalueParser- a PartialParser that returns aVvalue from a section ofstr
-
parse
public static <V> CaseInsensitiveOrderedMap<V> parse(String str, String entrySeparator, String keyValueSeparator, 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 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 valuevalueParser- a PartialParser that returns aVvalue from a section ofstrbrackets- if true, the first and last chars instrwill be ignored
-
parse
public static <V> CaseInsensitiveOrderedMap<V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<V> valueParser, int offset, int length) Creates a new map by parsing the given subrange ofstrwith the given PartialParser 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 valuevalueParser- 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
-