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?

Asked by: Rafael712 | Posted: 28-01-2022

Answer 1

TreeMap extends Map and supports custom comparators.

String provides a default case insensitive comparator.


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.

Answered by: Emma641 | Posted: 01-03-2022

Answer 2

You could use CaseInsensitiveMap from Apache's Commons Collections.

Answered by: Darcy238 | Posted: 01-03-2022

Answer 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) {

This approach does not force you to change your "key" type but your Map implementation.

Answered by: Marcus687 | Posted: 01-03-2022

Answer 4

You need a wrapper class for your String key with a case-insensitive equals() and hashCode() implementation. Use that instead of the String for the Map's key.

See an example implementation at I found it in 2 minutes of googling. Looks sensible to me, though I've never used it.

Answered by: Emily419 | Posted: 01-03-2022

Answer 5

The three obvious solutions that spring to mind:

  • Normalise the case before using a String as a key (not Turkish locale works differently from the rest of the world).

  • Use a special object type designed to be used as key. This is a common idiom for dealing with composite keys.

  • Use a TreeMap with a Comparator that is case insensitive (possibly a PRIMARY or SECONDARY strength java.text.Collator). Unfortunately the Java library doesn't have a Comparator equivalent for hashCode/equals.

Answered by: Anna616 | Posted: 01-03-2022

Answer 6

You could use my Apache licensed CaseInsensitiveMap discussed here. Unlike the Apache Commons version, it preserves the case of keys. It implements the map contract more strictly than TreeMap (plus has better concurrent semantics) (see the blog comments for details).

Answered by: Kelsey276 | Posted: 01-03-2022

Answer 7

Trove4j can use custom hashing for a HashMap. This may however have performance implications given that hashcodes cannot be cached (although Trove4j may have found a way around this?). Wrapper objects (as described by John M) do not have this caching deficiency. Also see my other answer regarding TreeMap.

Answered by: Ned456 | Posted: 01-03-2022

Answer 8

Check the accepted answer at the link below. How to check for key in a Map irrespective of the case?

Bottom line is "The simplest solution is to simply convert all inputs to uppercase (or lowercase) before inserting/checking. You could even write your own Map wrapper that would do this to ensure consistency."

Answered by: Lydia574 | Posted: 01-03-2022

Similar questions

java - How to convert a JSON string to a Map<String, String> with Jackson JSON

I'm trying to do something like this but it doesn't work: Map&lt;String, String&gt; propertyMap = new HashMap&lt;String, String&gt;(); propertyMap = JacksonUtils.fromJSON(properties, Map.class); But the IDE says: Unchecked assignment Map to Map&lt;String,String&gt; What's the right way to do this? I'm only using Jackson because th...

java - Parsing JSON into Map<String, Entity> with FlexJSON

I am trying to parse a JSON structure similar to this one: { "cars": { "112": { "make": "Cadillac", "model": "Eldorado", "year": "1998" }, "642": { "make": "Cadillac", "model": "Eldorado", "year": "1990" }, "9242": { "make": "Cadillac", "model": "Eldorado", "year": "2001" } }} I have a CarEntity class defined with makeName,model,year attributes ...

How to inject a Map<String, List> in java springs?

How to inject a Map in java spring framework? If possible please provide some sample code. Is the following legal? &lt;property name="testMap"&gt; &lt;map&gt; &lt;entry&gt; &lt;key&gt; &lt;value&gt;test&lt;/value&gt; &lt;/key&gt; &lt;value&gt; &lt;list&gt; &lt;val...

java - Why is key (or value) of Map<String, String> not returned as String?

In the following short code snippet, Eclipse flags an error about the String key = pair.getKey(); and the String value = pair.getValue(); statements, saying that "Type mismatch: cannot convert from Object to String" This is the relevant code: for (Map&lt;String, String&gt; dic : dics) { Iterator it = dic.entrySet().iterator...

java - How to convert Map<String, String> to Map<Long, String> ? (option : using guava)

I have a Map&lt;String, String&gt; the String key is nothing but numeric value like "123" etc. I'm getting numeric value because this values are coming from the UI in my JSF component. I don't want to change the contract of UI component. Now I would like to create a Map&lt;Long, String&gt; based on the above Map, I saw some transform methods in the

How to construct a complex json structure using Map<String, String> in java

I'm fighting a similar 403 error found in this question The summary is that I'm doing what should be a simple http POST w/ json data as the http body. Instead of a 200 response I'm getting 403 and before I dive deep into why I thought I would take the advice of the user in the question I r...

java - Mapping a Map<String, Double> with Hibernate and JPA

I try the following mapping @ElementCollection private Map&lt;String, Double&gt; doubleValues; But when I generate my schema from this (using mvn hibernate3:hbm2ddl), I get the following errors: org.hibernate.MappingException: Could not determine type for: java.util.Map, at table: ImagerAnalysisResult, for columns: [org.hibernate.mapping.Column(doubleValues...

java - want to create a map<String , Object> ,Object can be String and can be class Object

How want to create a Map&lt;String , Object&gt;. In this map everytime the Object is a string. But Now I want to put a class in the object in addition to that. Is this a good way to mix string and a class object? If yes, when I iterate through the map, how can I distiguish between class and string?

java - How to map a Map<String, int[]>?

Is it possible to map the data structure Map&lt;String, int[]&gt; using hibernate? The array will allways have the same number entries. Sorry for the possibly dumb question but I have currently no idea how to do this.

java - Putting into a Map<String, ?>

So I have a Map that has some values in it being passed into a method: public String doThis(Map&lt;String, ?&gt; context){ ..... } And I'm trying to insert an addition attribute to this Map String abc="123"; context.put("newAttr",abc); But I am getting this error: The method put(String, capture#8-of ?) in the type Map is not applicab...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)