Note that there are some explanatory texts on larger screens.

plurals
  1. POAppropriately backing a case insensitive map
    primarykey
    data
    text
    <p>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:</p> <pre><code>boolean preserve_case = true; Map&lt;String, MyClass&gt; maplet = new CaseInsensitiveHashMap&lt;MyClass&gt;(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&lt;Entry&lt;String, MyClass&gt;&gt; case_sensitive_set = maplet.entrySet(); // Since the client input true to preserve order, this entry set should be ["FoO"=my_class.toString()] </code></pre> <p>I can handle most of this pretty well; I simply keep a <code>HashMap</code> on the backend. When a client puts anything in, I uppercase the key before adding it to the map.</p> <p>I'm just having a hard time writing the <code>keySet()</code> and <code>entrySet()</code> methods. I want the returned entry set and key set to be backed by the map, as is the standard with Java maps.</p> <p>However, the only way I can think of handling this is to create a second backing data structure, something like a <code>preserved_case_map</code>, which contains the <code>input.toUpperCase()</code> => input as key value pairs. When the client calls for the <code>entrySet()</code> (or <code>keySet()</code>), I can construct the returned entry set by looping through the <code>preserved_case_map</code>. The problem here is that the returned entry set will not be modified if I make changes to the <code>HashMap</code>, unless I'm misunderstanding something...</p> <p>Let me know if this makes sense, or if I'm convoluting a simple situation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload