users@jsr311.java.net

Re: Case insensitive map

From: Reto Bachmann-Gmür <reto_at_gmuer.ch>
Date: Tue, 02 Sep 2008 08:13:06 +0200

Bill Burke wrote:
> I wrote a CaseInsensitiveMap. I don't see what the problem is.
1. To which object are instances of your class equals?
2. How to you calculate the hashCode?

If you do consider the case of the keys (i.e. use the keys as added)
this is against the concept of case-insensitiveness. Also you would have
two maps which result as different even though they answer the same
value for the same set of keys. If you take the hashCode of key in
specific casing you're making an arbitrary choice and it not longer
workds properly across different implementations (which is why
java.util.Map defines how these Methods are to be implemented).

java.util.Map.get(key) is defined as:
> More formally, if this map contains a mapping from a key k to a value
> v such that (key==null ? k==null : key.equals(k)), then this method
> returns v; otherwise it returns null. (There can be at most one such
> mapping.)

3. How do you implement your Map do satisfy this inherited requirement
and be case-insensitive for keys?

4. Do map.comtainsKey(k) and map.keySet().contains(k) return the same
value? (do you return a case-insensitive set)

Reto

>
> Reto Bachmann-Gmür wrote:
>> HttpHeaders.getRequestHeaders has to return a
>> MultivaluedMap<java.lang.String,java.lang.String> that is
>> case-insensitive with regards to keys. I'm not sure how such a case
>> insensitive map is supposed to work exactly (e.g. what does keySet
>> return?) and I think that it might be quite hard to have a definition
>> compatible the the API for the java.util.Map (which is extended by
>> MultivaluedMap).
>>
>> An issue is map identity. The javadoc for java.util.Map.equals says:
>> "Returns true if the given object is also a map and the two maps
>> represent the same mappings." as the mapping of keys with different
>> casing is the same I would think that two maps are equals is they
>> differ only by the casing of the keys, however the text more
>> specifically says: "More formally, two maps m1 and m2 represent the
>> same mappings if m1.entrySet().equals(m2.entrySet())." The latter
>> condition is only met if the EntryS contains the key with the same
>> casing (or if the keyset is expanded to contain possible casings of a
>> key), but this is not defined by the jax-rs spec.
>>
>> I think jax-rs should not introduce a case-insensitive map but rather
>> either specify that all-lower (or upper) case strings are to be used
>> as keys or introduce a class like "CaseInsensitiveString" used as key
>> in the map.
>>
>> Reto
>>
>