Case insensitive keys using Collections

fritz-bayer_at_web.de
Date: 01/16/05


Date: 16 Jan 2005 02:42:25 -0800

Hi,

I'm looking for container (hashmap or set), into which I can put key
value objects that I can latter retrieve using a string as a key in a
case insensitive manner.

It's important that the original key stays unchanged. For example, it's
NOT ok to transform the key in the key value object into upper or
lower.

Here is some hypothetical code to illustrate the requested behaviour:

// This is our case insensitive hashmap.
CaseinsenstiveHashMap hm = new CaseinsenstiveHashMap();

KeyValueObject keyvalue = new KeyValueObject();
keyvalue.setKey("coNtent-LengtH", "12024");

hm.put(keyValue.getKey(), keyValue);

// Keys are case insenstive !
assertEquals("12024",
((KeyValueObject)hm.get("Content-Lenght")).getValue() );
assertEquals("12024",
((KeyValueObject)hm.get("CONTENT-LENGTH")).getValue() );
assertEquals("12024",
((KeyValueObject)hm.get("cOnTeNt-lEnGtH")).getValue() );

// The original key is unchanged !
assertEquals("coNtent-LengtH",((KeyValueObject)hm.get("cOnTeNt-lEnGtH")).getKey());

I tried overwriting the hashCode() and equals() fucntion but then I
won't be able to retrieve the objects using a simple String in the
get("simpleString") function of the hashtable!

So I guess I have to use the Comparator interface to compare Strings
with KeyValueObjects?

But I'm not sure, whether or not the Comparator is also being used when
retrieveing objects. Isn't it only consulted when inserting new
elements to preserve order?

Fritz



Relevant Pages

  • Is HashMap slow?
    ... I store the properties/db fields in a ... So to retrieve the value I use: ... String value = fields.get; ... I pass the HashMap on to a generic Database table class. ...
    (comp.lang.java.programmer)
  • Re: Getting command line of another process
    ... It seems to be very complicated code just to retrieve the command line of a process. ... static public bool RetrieveUserModeProcessString(IntPtr HandleProcess, IntPtr PageBaseAddress, UInt32 UnicodeStringOffset, out string Result) ... UInt32 UnicodeStringSize = 8; ... pStringContent = Marshal.AllocCoTaskMemStringLength); ...
    (microsoft.public.win32.programmer.kernel)
  • Very simple Java problem, please help
    ... delete latent messages and nicknames that linger on the server more ... I set up a few JSP scripts that will retrieve and remove these files by ... @param String fileName ... Vector contentVector = getFileContents; ...
    (comp.lang.java.help)
  • Re: Getting command line of another process
    ... It seems to be very complicated code just to retrieve the command line of a ... HandleProcess, IntPtr PageBaseAddress, UInt32 UnicodeStringOffset, out string ... pUnicodeStringAddress, pUnicodeString, UnicodeStringSize, out ... pStringContent = Marshal.AllocCoTaskMemStringLength); ...
    (microsoft.public.win32.programmer.kernel)
  • Very simple Java problem, please help
    ... delete latent messages and nicknames that linger on the server more ... I set up a few JSP scripts that will retrieve and remove these files by ... @param String fileName ... Vector contentVector = getFileContents; ...
    (comp.lang.java.programmer)