Is there a good way to have a Map<String, ?> get and put ignoring case? [duplicate]
Solution 1:
TreeMap extends Map and supports custom comparators.
String provides a default case insensitive comparator.
So:
final Map<String, ...> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
The comparator does not take locale into account. Read more about it in its JavaDoc.
Solution 2:
You could use CaseInsensitiveMap from Apache's Commons Collections.
Solution 3:
Would it be possible to implement your own Map overriding put/get methods ?
public class CaseInsensitiveMap extends HashMap<String, String> {
...
put(String key, String value) {
super.put(key.toLowerCase(), value);
}
get(String key) {
super.get(key.toLowercase());
}
}
This approach does not force you to change your "key" type but your Map implementation.