Forums Register Login

HashMap key...case insensitivity for get()?

+Pie Number of slices to send: Send
Hello. i am coding a web service that processes a hashmap. I'm able to parse the map ok and use conditional ifs to determine what the current key is via string.equalsIgnoreCase(...).

However, I need to use a hashmap.get(key_name) for one operation and the key_name argument is case sensitive. Since this is a web service and i have no real control over how the consumers are sending the keys, i run a high risk of throwing a NullPointerException if the case of key_name uploaded by the consumer does not match the case of key_name in the service's get method.

is there anyway to make the HashMap.get method case insensitive. The only alternative i can think of is to scroll the entire hashmap in a mini- nested loop using conditional string.equalsIgnoreCase but that seems so redundant. Thank you very much for reading this.
[ August 07, 2008: Message edited by: Tom Griffith ]
+Pie Number of slices to send: Send
How are values put into the map? If you do it in the webservice can you call toUpperCase() on the key before calling put() and do similar before calling get(). This way you dont need to care about the case the user entered it....
+Pie Number of slices to send: Send
hi. the consumers are populating the map and it's uploaded as a method argument to the service...i guess i can't guarantee how any of them will send the key.
+Pie Number of slices to send: Send
The easiest way is: every time you put a key into the map, call toLowerCase() on the key first. And every time you do a get, again call toLowerCase() on the key. If you need to make the Map directly available to others who can't be trusted to do this themselves, you can create a decorator to wrap your map with methods that do this automatically, e.g.
+Pie Number of slices to send: Send
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 28820 times.
Similar Threads
naming objects at runtime
Dynamic method resolution
HashMap doubt?
Difference between Properties class and HashTable,HashMap
HashMap
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:06:36.