| Author |
how to search equalignorecase search with hashmap
|
vijayakumar durai
Ranch Hand
Joined: Aug 18, 2008
Posts: 153
|
|
Hi friends,
I am adding company name in the hash map .I have text box .If user type 'A' or 'a' I have to display 'A' or 'a' company to the user.can anybody tell how to search equalignorecase search in hashmap?
My code
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4201
|
|
1. Convert to lowercase before adding to the Map
2. Convert the search String to lowercase and search on that.
Or use uppercase for both. Whatever.
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Ganesh Gowtham
Ranch Hand
Joined: Mar 30, 2005
Posts: 223
|
|
If you want to preserve the "case" in Hashmap's Key , then while searching use equalsIgnoreCase(...)
|
Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
|
 |
vijayakumar durai
Ranch Hand
Joined: Aug 18, 2008
Posts: 153
|
|
I want to search equalignorecase on hashmap value can anybody give example I tried but its not working
I am searching value here
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
|
If you are adding numbers to the key, you will have to record the numbers, otherwise you will never find the key again. Using "companyname123" as a key looks like bad design to me. In fact it sounds as if you are not using the Map as a Map. Maybe you ought to use a List and simply look for item 123.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Use a TreeMap<String>(String.CASE_INSENSITIVE_ORDER). This will use a Comparator<String> that ignores the case. Note that, as the API indicates, this is actually a breach of the Map contract:
I said currently, because it all depends on the way equals is implemented. If the method loops over its own contents and checks if the other map has the same key/value pairs, then you get this. If the method loops over the other map then the output will be reversed.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Ganesh Gowtham
Ranch Hand
Joined: Mar 30, 2005
Posts: 223
|
|
Rob Prime wrote:Use a TreeMap<String>(String.CASE_INSENSITIVE_ORDER). This will use a Comparator<String> that ignores the case. Note that, as the API indicates, this is actually a breach of the Map contract:
I said currently, because it all depends on the way equals is implemented. If the method loops over its own contents and checks if the other map has the same key/value pairs, then you get this. If the method loops over the other map then the output will be reversed.
Hi rob
Map<String,String> map2 = new TreeMap<String,String>(String.CASE_INSENSITIVE_ORDER);
Thanks for showing another way of doing
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
You're welcome.
|
 |
 |
|
|
subject: how to search equalignorecase search with hashmap
|
|
|