CrypticCabub CrypticCabub - 1 year ago 150
Java Question

CaseInsensitive Map Key Java

I have a

Map<String, Object>
in which I need the String key to be case insensitive

Currently I am wrapping my
objects in a Wrapper class I've called
the code for which looks like this:

* A string wrapper that makes .equals a caseInsensitive match
* <p>
* a collection that wraps a String mapping in CaseInsensitiveStrings will still accept a String but will now
* return a caseInsensitive match rather than a caseSensitive one
* </p>
public class CaseInsensitiveString {
String str;

private CaseInsensitiveString(String str) {
this.str = str;

public static CaseInsensitiveString wrap(String str) {
return new CaseInsensitiveString(str);

public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;

if(o.getClass() == getClass()) { //is another CaseInsensitiveString
CaseInsensitiveString that = (CaseInsensitiveString) o;
return (str != null) ? str.equalsIgnoreCase(that.str) : that.str == null;
} else if (o.getClass() == String.class){ //is just a regular String
String that = (String) o;
return str.equalsIgnoreCase(that);
} else {
return false;


public int hashCode() {
return (str != null) ? str.toUpperCase().hashCode() : 0;

public String toString() {
return str;

I was hoping to be able to get a
Map<CaseInsensitiveString, Object>
to still accept a
and return the value without having to do
. In my testing however, my
has returned null whenever I have tried to do this but it does work if I wrap the String before calling

Is it possible to allow my
to accept both String and
parameters to the get method and work in a caseInsensitive fashion regardless of if the
is wrapped or not, and if so, what am I doing wrong?

for reference my test code looks like this:

Map<CaseInsensitiveString, String> test = new HashMap<>();
test.put(CaseInsensitiveString.wrap("TesT"), "value");

and returns:


Answer Source

You can do it like this:

Map<String, Object> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

See this question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download