Case insensitive string as HashMap key
Map<String, String> nodeMap =
new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
That's really all you need.
As suggested by Guido García in their answer here:
import java.util.HashMap;
public class CaseInsensitiveMap extends HashMap<String, String> {
@Override
public String put(String key, String value) {
return super.put(key.toLowerCase(), value);
}
// not @Override because that would require the key parameter to be of type Object
public String get(String key) {
return super.get(key.toLowerCase());
}
}
Or
https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/map/CaseInsensitiveMap.html
One approach is to create a custom subclass of the Apache Commons AbstractHashedMap
class, overriding the hash
and isEqualKeys
methods to perform case insensitive hashing and comparison of keys. (Note - I've never tried this myself ...)
This avoids the overhead of creating new objects each time you need to do a map lookup or update. And the common Map
operations should O(1) ... just like a regular HashMap
.
And if you are prepared to accept the implementation choices they have made, the Apache Commons CaseInsensitiveMap
does the work of customizing / specializing AbstractHashedMap
for you.
But if O(logN) get
and put
operations are acceptable, a TreeMap
with a case insensitive string comparator is an option; e.g. using String.CASE_INSENSITIVE_ORDER
.
And if you don't mind creating a new temporary String object each time you do a put
or get
, then Vishal's answer is just fine. (Though, I note that you wouldn't be preserving the original case of the keys if you did that ...)
Subclass HashMap
and create a version that lower-cases the key on put
and get
(and probably the other key-oriented methods).
Or composite a HashMap
into the new class and delegate everything to the map, but translate the keys.
If you need to keep the original key you could either maintain dual maps, or store the original key along with the value.
Two choices come to my mind:
- You could use directly the
s.toUpperCase().hashCode();
as the key of theMap
. - You could use a
TreeMap<String>
with a customComparator
that ignore the case.
Otherwise, if you prefer your solution, instead of defining a new kind of String, I would rather implement a new Map with the required case insensibility functionality.