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

how to ignore String case when checking through Set keys?

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





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.

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create an implementation of Set<String> which ignores case in the contains() method.

 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, just as fast as Garrett's, but with less code:
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you guys. i got an idea from your replies.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic