Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good way to have a Map<String, ?> get and put ignoring case? [duplicate]

Is there a good way to have a Map<String, ?> get and put ignoring case?

like image 285
Joshua Avatar asked Oct 17 '08 15:10

Joshua


People also ask

How do you ignore a case on a Map?

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.

Is Map containsKey case sensitive?

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.

Is containsKey case sensitive apex?

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.


2 Answers

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.

like image 174
volley Avatar answered Sep 22 '22 16:09

volley


You could use CaseInsensitiveMap from Apache's Commons Collections.

like image 32
Eric Weilnau Avatar answered Sep 21 '22 16:09

Eric Weilnau