Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to search equalignorecase search with hashmap

 
vijayakumar durai
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to preserve the "case" in Hashmap's Key , then while searching use equalsIgnoreCase(...)
 
vijayakumar durai
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic