Reputation: 57
I have a list> and now I want to search the key without considering case. i.e. map.get(0).get("test") or map.get(0).get("TEST"). both result should give the value for key 'TESt'.
I am filling the List> list from other list> like below. There is no way to change the original map. Can anyone tell me how to add key with upper case?
mappedListHashed is List> accessLvlArray is List>
for (HashMap<String, String> map : mappedListHashed) {
accessLvlArray.add(map);
}
Upvotes: 2
Views: 578
Reputation: 172418
You can try to override the put method:
public class myMap extends HashMap<String, String> {
@Override
public String put(String key, String value) {
return super.put(key.toLowerCase(), value);
}
public String get(String key) {
return super.get(key.toLowerCase());
}
}
Upvotes: 1
Reputation: 393791
You could always put just lower case keys in your Map
list.get(0).put("tEst".toLowerCase(),"value");
and call
list.get(0).get("Test".toLowerCase())
in order to always search for the lower case version of the key.
If that's not an option, you can wrap your String key with a custom class that overrides equals
and hashCode
in a manner that ignores case.
Upvotes: 3