F
fritz-bayer
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
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