Case-Insensitive Map?

I have a Map object containing String keys and String values. I then pull out the required value using the key i.e. map.get(key); The problem is the case of the String key i use to lookup an item in the Map may not match the case of a key in the Map. So i want to do a lookup while ignoring the case of the key. Is this possible?

[336 byte] By [sanh2005a] at [2007-11-26 15:23:12]
# 1

> I have a Map object containing String keys and String

> values. I then pull out the required value using the

> key i.e. map.get(key); The problem is the case of

> the String key i use to lookup an item in the Map may

> not match the case of a key in the Map. So i want to

> do a lookup while ignoring the case of the key. Is

> this possible?

Do a toLowerCase() (or toUpperCase(), whichever you prefer) when putting your keys. Do the same thing when getting. You can also write a simple class that encapsulates and abstracts away this behavior for case-insensitive gets.

~

yawmarka at 2007-7-8 21:38:22 > top of Java-index,Java Essentials,Java Programming...
# 2

nope. what you could do, though, is implement your own Map class, and in the "put" method, first call toLowerCase() on the key, then in the "get" method, first call toLowerCase() on the key again. give you pretty much what you're after

be careful that this is really what you want to do, though. it's got the potential to behave very oddly if your code isn't really case insensitive

georgemca at 2007-7-8 21:38:22 > top of Java-index,Java Essentials,Java Programming...
# 3

> I have a Map object containing String keys and String

> values. I then pull out the required value using the

> key i.e. map.get(key); The problem is the case of

> the String key i use to lookup an item in the Map may

> not match the case of a key in the Map. So i want to

> do a lookup while ignoring the case of the key. Is

> this possible?

No,

It's better to convert all keys to upper or lower case when you insert data, and then use upper or lower case when you search for data.

Kaj

kajbja at 2007-7-8 21:38:23 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks for the quick replies, i will look at the ideas suggested.
sanh2005a at 2007-7-8 21:38:23 > top of Java-index,Java Essentials,Java Programming...
# 5

You can use a TreeMap with a case-insensitive comparator.

Map map = new TreeMap(String.CASE_INSENSTIVE_ORDER);

m.put("abc", new Integer(1));

m.put("Abc", new Integer(2));

print(m.get("ABC"); // 2

jsalonena at 2007-7-8 21:38:23 > top of Java-index,Java Essentials,Java Programming...
# 6
> You can use a TreeMap with a case-insensitive comparator.Even better!<obligatory-nitpick-about-code-that-doesn't-compile />~
yawmarka at 2007-7-8 21:38:23 > top of Java-index,Java Essentials,Java Programming...
# 7

It might not be sufficient though, for example the sz-ligature ?used in German is written SS in upper case, but they are considered different by this comparator. Standardizing on either lower or upper case keys is safer. Or you could use a locale-specific Collator to compare.

> <obligatory-nitpick-about-code-that-doesn't-compile />

ups.. I should be more careful when copying and pasting

jsalonena at 2007-7-8 21:38:23 > top of Java-index,Java Essentials,Java Programming...