I came across this post for a case insensitive hashmap and tried to implement it but I'm not getting the expected result. For some reason it's not returning the value when I do get with a different casing and is returning null, and I thought that you didn't really need a non-default constructor in this case but I'm not sure.
public class CaseInsensitiveMap extends HashMap<String, Integer> {
@Override
public Integer put(String key, Integer value) {
return super.put(key.toLowerCase(), value);
}
// not @Override because that would require the key parameter to be of type Object
public Integer get(String key) {
return super.get(key.toLowerCase());
}
}
and used like so;
HashMap<String, Integer> stuff = new CaseInsensitiveMap();
stuff.put("happy", 11);
System.out.println(stuff);
Integer result = stuff.get("HAPPy");
System.out.println(result);
System.out.println(stuff);
but result is;
{happy=11}
null
{happy=11}