This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes how to ignore String case when checking through Set keys? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to ignore String case when checking through Set keys?" Watch "how to ignore String case when checking through Set keys?" New topic
Author

how to ignore String case when checking through Set keys?

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844





I want that i check through s.contains(str). It should ignnore case. So i can print the contents for Set.

One possible way is this,
any other suggestions



I am looking forward to implement something like equalsIgnoreCase when check some key's in Set.


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You could create an implementation of Set<String> which ignores case in the contains() method.



Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9

That won't work if the set contains a string with mixed case, like "Amandeep". You're checking for "amandeep" and "AMANDEEP", but not other possibilities. Garrett's suggestion will be much more reliable.

Perhaps the simplest way to code this would be to use a TreeSet with a custom comparator:

That's probably not as fast as Garrett's version, but it works, and it's complete. I think.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
Or, just as fast as Garrett's, but with less code:
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I thought about that, but I wanted to at least keep the property of the iterator retuning the Strings in the case that they were originally inserted. The OP didn't say that was necessary, but it was a fun thought experiment. It does require a heck of a lot more code though.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Mike Simmons wrote:

That won't work if the set contains a string with mixed case, like &quot;Amandeep&quot;. You're checking for &quot;amandeep&quot; and &quot;AMANDEEP&quot;, but not other possibilities. Garrett's suggestion will be much more reliable.

Perhaps the simplest way to code this would be to use a TreeSet with a custom comparator:

That's probably not as fast as Garrett's version, but it works, and it's complete. I think.


thanks for your reply. the problem you mentioned, i got aware of.

As per my problem, i can't add to set in lowercase, as i might be using the set contents at later stage. so i want them intact.

this workout will work for me.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
Amandeep wrote:As per my problem, i can't add to set in lowercase, as i might be using the set contents at later stage. so i want them intact.

Well, Garrett's original code handles this fine, as does my TreeSet solution. Both are considerably faster than your iterate-through-everything solution. But as long as the set is not too large, performance need not be an issue. You could also use a HashMap<String, String>, where the key is forced to be toLowerCase(), but the value retains the original capitalization. That might be the best of all possibilities here.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Mike Simmons wrote:Perhaps the simplest way to code this would be to use a TreeSet with a custom comparator:

That's probably not as fast as Garrett's version, but it works, and it's complete. I think.

Actually, String already has a comparator for that: CASE_INSENSITIVE_ORDER.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
thank you guys. i got an idea from your replies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to ignore String case when checking through Set keys?
 
Similar Threads
generating unique Random Numbers
Why am able to pass a Set (non-generic) to a method expecting Set<Number>?
HashSet Magic
Set.toArray
How to ignore case in Set ?