Class CaseInsensitiveMap<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- org.apache.commons.collections4.map.AbstractHashedMap<K,V>
-
- org.apache.commons.collections4.map.CaseInsensitiveMap<K,V>
-
- Type Parameters:
K
- the type of the keys in this mapV
- the type of the values in this map
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.util.Map<K,V>
,Get<K,V>
,IterableGet<K,V>
,IterableMap<K,V>
,Put<K,V>
public class CaseInsensitiveMap<K,V> extends AbstractHashedMap<K,V> implements java.io.Serializable, java.lang.Cloneable
A case-insensitiveMap
.Before keys are added to the map or compared to other existing keys, they are converted to all lowercase in a locale-independent fashion by using information from the Unicode data file.
Null keys are supported.
The
keySet()
method returns all lowercase keys, or nulls.Example:
Map<String, String> map = new CaseInsensitiveMap<String, String>(); map.put("One", "One"); map.put("Two", "Two"); map.put(null, "Three"); map.put("one", "Four");
The example above creates a
CaseInsensitiveMap
with three entries.map.get(null)
returns"Three"
andmap.get("ONE")
returns"Four".
TheSet
returned bykeySet()
equals{"one", "two", null}.
This map will violate the detail of various Map and map view contracts. As a general rule, don't compare this map to other maps. In particular, you can't use decorators like
ListOrderedMap
on it, which silently assume that these contracts are fulfilled.Note that CaseInsensitiveMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. The simplest approach is to wrap this map using
Collections.synchronizedMap(Map)
. This class may throw exceptions when accessed by concurrent threads without synchronization.- Since:
- 3.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.commons.collections4.map.AbstractHashedMap
AbstractHashedMap.EntrySet<K,V>, AbstractHashedMap.EntrySetIterator<K,V>, AbstractHashedMap.HashEntry<K,V>, AbstractHashedMap.HashIterator<K,V>, AbstractHashedMap.HashMapIterator<K,V>, AbstractHashedMap.KeySet<K>, AbstractHashedMap.KeySetIterator<K>, AbstractHashedMap.Values<V>, AbstractHashedMap.ValuesIterator<V>
-
-
Field Summary
Fields Modifier and Type Field Description private static long
serialVersionUID
Serialisation version-
Fields inherited from class org.apache.commons.collections4.map.AbstractHashedMap
data, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD, entrySet, GETKEY_INVALID, GETVALUE_INVALID, keySet, loadFactor, MAXIMUM_CAPACITY, modCount, NO_NEXT_ENTRY, NO_PREVIOUS_ENTRY, NULL, REMOVE_INVALID, SETVALUE_INVALID, size, threshold, values
-
-
Constructor Summary
Constructors Constructor Description CaseInsensitiveMap()
Constructs a new empty map with default size and load factor.CaseInsensitiveMap(int initialCapacity)
Constructs a new, empty map with the specified initial capacity.CaseInsensitiveMap(int initialCapacity, float loadFactor)
Constructs a new, empty map with the specified initial capacity and load factor.CaseInsensitiveMap(java.util.Map<? extends K,? extends V> map)
Constructor copying elements from another map.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CaseInsensitiveMap<K,V>
clone()
Clones the map without cloning the keys or values.protected java.lang.Object
convertKey(java.lang.Object key)
Overrides convertKey() fromAbstractHashedMap
to convert keys to lower case.private void
readObject(java.io.ObjectInputStream in)
Read the map in using a custom routine.private void
writeObject(java.io.ObjectOutputStream out)
Write the map out using a custom routine.-
Methods inherited from class org.apache.commons.collections4.map.AbstractHashedMap
addEntry, addMapping, calculateNewCapacity, calculateThreshold, checkCapacity, clear, containsKey, containsValue, createEntry, createEntrySetIterator, createKeySetIterator, createValuesIterator, destroyEntry, doReadObject, doWriteObject, ensureCapacity, entryHashCode, entryKey, entryNext, entrySet, entryValue, equals, get, getEntry, hash, hashCode, hashIndex, init, isEmpty, isEqualKey, isEqualValue, keySet, mapIterator, put, putAll, remove, removeEntry, removeMapping, reuseEntry, size, toString, updateEntry, values
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Serialisation version- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CaseInsensitiveMap
public CaseInsensitiveMap()
Constructs a new empty map with default size and load factor.
-
CaseInsensitiveMap
public CaseInsensitiveMap(int initialCapacity)
Constructs a new, empty map with the specified initial capacity.- Parameters:
initialCapacity
- the initial capacity- Throws:
java.lang.IllegalArgumentException
- if the initial capacity is negative
-
CaseInsensitiveMap
public CaseInsensitiveMap(int initialCapacity, float loadFactor)
Constructs a new, empty map with the specified initial capacity and load factor.- Parameters:
initialCapacity
- the initial capacityloadFactor
- the load factor- Throws:
java.lang.IllegalArgumentException
- if the initial capacity is negativejava.lang.IllegalArgumentException
- if the load factor is less than zero
-
CaseInsensitiveMap
public CaseInsensitiveMap(java.util.Map<? extends K,? extends V> map)
Constructor copying elements from another map.Keys will be converted to lower case strings, which may cause some entries to be removed (if string representation of keys differ only by character case).
- Parameters:
map
- the map to copy- Throws:
java.lang.NullPointerException
- if the map is null
-
-
Method Detail
-
convertKey
protected java.lang.Object convertKey(java.lang.Object key)
Overrides convertKey() fromAbstractHashedMap
to convert keys to lower case.Returns
AbstractHashedMap.NULL
if key is null.- Overrides:
convertKey
in classAbstractHashedMap<K,V>
- Parameters:
key
- the key convert- Returns:
- the converted key
-
clone
public CaseInsensitiveMap<K,V> clone()
Clones the map without cloning the keys or values.- Overrides:
clone
in classAbstractHashedMap<K,V>
- Returns:
- a shallow clone
-
writeObject
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
Write the map out using a custom routine.- Parameters:
out
- the output stream- Throws:
java.io.IOException
- if an error occurs while writing to the stream
-
readObject
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
Read the map in using a custom routine.- Parameters:
in
- the input stream- Throws:
java.io.IOException
- if an error occurs while reading from the streamjava.lang.ClassNotFoundException
- if an object read from the stream can not be loaded
-
-