Posted By:
Christopher_Schultz
Posted On:
Thursday, January 10, 2002 05:22 AM
So, you need a HashMap which is case-insensitive with respect to the key, but you need to preserve the case when reading the keys?
You'll have to write your own HashMap implementation, then. If you didn't care about the case of the keys and could store them, say, in lower-case only, then you could subclass the existing one:
public class CaseInsensitiveHashMap
extends HashMap
{
public void put(Object key, Object value)
{
if(!(key instanceof String))
throw new IllegalArgumentException("Key must be a String");
super.put(((String)key).toLowerCase(), value);
}
public Object get(Object key)
{
if(!(key instanceof String))
throw new IllegalArgumentException("Key must be a String");
return(super.get(((String)key).toLowerCase()));
}
}
This Map may break some of the contract of the Map class -- you'll have to document that so that client code knows what to expect.
Hope that helps,
-chris