Saturday, December 4, 2010

How to create Case Insensitive Map in Java

In order to create case insensitive map, just extend HashMap class and override get and put method. Using the below code you can give keys irrespective of case.
class CaseImap extends HashMap<String, Object>
{
// Overriding get method
public String
get(String key)
{
return (String)super.get(key.toLowerCase());
}
// Overriding put method
public Object
put(String key, Object value)
{
return super.put(key.toLowerCase(), value);
}
}

No comments:

Post a Comment