File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to search equalignorecase search with hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to search equalignorecase search with hashmap" Watch "how to search equalignorecase search with hashmap" New topic
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: 4638
    
    5

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: 225

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: 39388
    
  28
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: 19718
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

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: 19718
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to search equalignorecase search with hashmap