• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to search equalignorecase search with hashmap

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22789
131
Eclipse IDE Spring Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22789
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic