views:

1526

answers:

7

Is there a good way to have a Map get and put ignore case?

+11  A: 

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 http://www.java.happycodings.com/Java_Util_Package/code3.html I found it in 2 minutes of googling. Looks sensible to me, though I've never used it.

John M
A little tricky, isn't it? I don't like to implement equals, and hashCode avove all, if it is not really really needed. I prefer the aproach of creating a Map implementation overriding put and get methods.
Guido
Creating a wrapper class is done all the time and is not tricky at all. The wrapper class solution is also much cleaner that overriding put, get, putAll. You can use the wrapper class on any hashed or equals based collection (HashSet, HashMap, ArrayList, etc).
Steve Kuo
The portability among different collections is interesting. Thank you. I didn't think about it. I suppose each approach has its advantages and drawbacks.
Guido
Another good reason to use the wrapper class is that if you want to preserve the original case of the string, it's impossible to do it with the override of the put.
AngeDeLaMort
+10  A: 

You could use org.apache.commons.collections.map.CaseInsensitiveMap from Apache's Commons Collections.

Eric Weilnau
+5  A: 

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) {
       super.get(key.toLowercase());
    }
}

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

Guido
That is basically the approach taken in Apache's Commons Collections CaseInsensitiveMap.
Eric Weilnau
Remember to use a locale when doing upper and lower case operations.
marcospereira
Don't forget to override all other "put" methods such as putAll.
Steve Kuo
One strange side-effect is that if you list the keys in this map, they will suddenly have turned lowercase.
volley
+2  A: 

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.

Tom Hawtin - tackline
A: 

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).

Glyn Normington
A: 

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.

volley
A: 

TreeMap extends Map and supports custom comparators.

String provides a default case insensitive comparator.

So:

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