I want to implement a case insensitive hash map. This question by itself isn't new, but I wanted to add extra functionality and don't know what general direction to take. I want the client to be able to do something like this:
boolean preserve_case = true;
Map<String, MyClass> maplet = new CaseInsensitiveHashMap<MyClass>(preserve_case); // If the client enters true at construction, then the put, get, and remove methods should still be case insensitive, but the entry and key sets should preserve the case that the client used when calling put.
maplet.put("FoO", my_class);
MyClass bar = maplet.get("foo"); // Should return a reference to my_class
Set<Entry<String, MyClass>> case_sensitive_set = maplet.entrySet(); // Since the client input true to preserve order, this entry set should be ["FoO"=my_class.toString()]
I can handle most of this pretty well; I simply keep a HashMap
on the backend. When a client puts anything in, I uppercase the key before adding it to the map.
I'm just having a hard time writing the keySet()
and entrySet()
methods. I want the returned entry set and key set to be backed by the map, as is the standard with Java maps.
However, the only way I can think of handling this is to create a second backing data structure, something like a preserved_case_map
, which contains the input.toUpperCase()
=> input as key value pairs. When the client calls for the entrySet()
(or keySet()
), I can construct the returned entry set by looping through the preserved_case_map
. The problem here is that the returned entry set will not be modified if I make changes to the HashMap
, unless I'm misunderstanding something...
Let me know if this makes sense, or if I'm convoluting a simple situation.