Is there a good way to have a Map<String, ?>
get and put ignoring case?
Therefore, if we provide a case-insensitive String Comparator, we'll get a case-insensitive TreeMap. which we can supply in the constructor: Map<String, Integer> treeMap = new TreeMap<>(String. CASE_INSENSITIVE_ORDER); treeMap.
I want to know whether a particular key is present in a HashMap, so i am using containsKey(key) method. But it is case sensitive ie it does not returns true if there is a key with Name and i am searching for name.
Map keys of type String are case-sensitive. Two keys that differ only by the case are considered unique and have corresponding distinct Map entries. Subsequently, the Map methods, including put , get , containsKey , and remove treat these keys as distinct.
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.
You could use CaseInsensitiveMap from Apache's Commons Collections.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With