• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!