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

8

64

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

Martica answered 17/10, 2008 at 15:7 Comment(1)
Similar question: #8237445Hightension
H
72

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.

Hayley answered 9/12, 2009 at 20:55 Comment(2)
Note that you don't need to worry about locale if you control the keys that are being used and not your end users, and your keys are english.Hilariahilario
If you need a Map that allows nulls, which TreeMap does not support, try the CaseInsenstiveMap at github.com/jdereg/java-util. This one accesses the Map in O(1) unlike TreeMap which is O(log n).Pugilist
S
43

You could use CaseInsensitiveMap from Apache's Commons Collections.

Strychnine answered 17/10, 2008 at 15:16 Comment(2)
note that CaseInsensitiveMap is not generic, so you cannot do Map<String, ?> with that.Bib
As of release 4.0, Apache's Commons Collections is generic.Strychnine
H
33

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.

Halland answered 17/10, 2008 at 15:23 Comment(9)
That is basically the approach taken in Apache's Commons Collections CaseInsensitiveMap.Strychnine
Remember to use a locale when doing upper and lower case operations.Heartbreaking
Don't forget to override all other "put" methods such as putAll.Nancynandor
One strange side-effect is that if you list the keys in this map, they will suddenly have turned lowercase.Hayley
Don't like the idea of extending HashMap, why not make a delegator like: CaseInsensitiveMap<V> which implements Map<String, V>, and that takes a Map<String, V> as a constructor argument?Ruyter
Don't forget containsKey and mind problems if you transfer its contents into another map.Ddene
It'd be just great if you marked volley's as the accepted answer.Phylloxera
Beware of doing this in JDK8. The implementation has changed and you now need to override get, put and putAll in order for this to be consistent.Envious
Please warn users here about the danger of locale-sensitive toLowercase() issues!Manassas
A
14

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.

Atwell answered 17/10, 2008 at 15:11 Comment(4)
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.Halland
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).Nancynandor
The portability among different collections is interesting. Thank you. I didn't think about it. I suppose each approach has its advantages and drawbacks.Halland
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.Tansy
A
3

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.

Albertalberta answered 17/10, 2008 at 15:46 Comment(0)
D
3

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

Description answered 30/4, 2009 at 14:23 Comment(0)
H
0

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.

Hayley answered 9/12, 2009 at 20:54 Comment(0)
S
0

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

Shipper answered 29/3, 2013 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.