AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Case-insensitive collections (sets, maps, etc.)

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java
View previous topic :: View next topic  
Author Message
Matt
Guest





PostPosted: Thu Mar 15, 2007 7:10 am    Post subject: Case-insensitive collections (sets, maps, etc.) Reply with quote



I have to believe I'm not the first person to have this question.
However, I'm not having any luck finding an answer.

I'm using Java 1.5. Say I have the following:

Set<String> a = new HashSet<String>();
Map<String, Object> b = new TreeMap<String, Object>();

Assume somewhere these are populated.

When I check the set for a string using a.contains("bob"), the method is
checking using a case-sensitive search. Similarly, if I did
b.containsKey("tom"), it's case-sensitive.

Is it possible to get these methods to perform a comparison
case-insensitively? Thus, performing:

a.contains("bob")
a.contains("BoB")
a.contains("BOB")

would all yield the same result?


Thanks for your time. Cheers,

Matt
--
Back to top
Lew
Guest





PostPosted: Thu Mar 15, 2007 7:10 am    Post subject: Re: Case-insensitive collections (sets, maps, etc.) Reply with quote



Please do not multi-post. If you truly must reach multiple groups, cross-post
instead. This will unify your thread so that people don't have to jump around
all over the place to help you. Why put obstacles in people's path?

Matt wrote:
Quote:
I have to believe I'm not the first person to have this question.
However, I'm not having any luck finding a conclusive answer.

FYI, I'm coding in Java 1.5. Say I have the following:

Set<String> a = new HashSet<String>();
Map<String, Object> b = new TreeMap<String, Object>();

Assume somewhere these are populated.

When I check the set for a string using a.contains("bob"), the method is
checking using a case-sensitive search. Similarly, if I did
b.containsKey("tom"), it's case-sensitive.

Is it possible to get these methods to perform a comparison
case-insensitively? Thus, performing:

Define a class wrapping String with a natural order based on case-insensitive
comparison. Use that as a base type in your Collections instead of String.

-- Lew
Back to top
Matt
Guest





PostPosted: Thu Mar 15, 2007 6:58 pm    Post subject: Re: Case-insensitive collections (sets, maps, etc.) Reply with quote



Tom,

Thanks much - I'll give that a try. String.CASE_INSENSITIVE_ORDER will
probably do the trick; I wasn't aware of that. I don't have a real need
to use hashes over trees - the TreeSet/TreeMap should work just as well
in my application.

Thanks everyone. Per Corona4456, the reason I'm not using
toLowerCase(), etc. is to preserve the original case for display. As
Tom suggested below, I could in fact use a secondary map to hash the
case-sensitive keys to a case-insensitive form.

Lew - thanks for the note. I hardly ever post on the newsgroups (first
time on the *java* newsgroup), so I didn't think to cover both bases
with a single message, nor that it would make a big difference.

Thanks all. Cheers,

Matt
--

Tom Hawtin wrote:
Quote:
Matt wrote:

Set<String> a = new HashSet<String>();
Map<String, Object> b = new TreeMap<String, Object>();

Assume somewhere these are populated.

When I check the set for a string using a.contains("bob"), the method
is checking using a case-sensitive search. Similarly, if I did
b.containsKey("tom"), it's case-sensitive.

You can set up a TreeSet or TreeMap to do it quite easily. use
String.CASE_INSENSITIVE_ORDER as the Comparator.

For a HashSet/HashMap, "bob", "Bob" and "bOb" would all be in completely
different places. So if each were possible as a key, you would need to
search the entire collection. Either write your own loop, or dump it
into a temporary TreeMap/TreeSet.

The other obvious solution for unsorted collections, is to canonicalise
the data first. Even if you wanted to preserve case for a Set, use
HashMap<String,String> keyed on, say, the lowercase form, and mapping to
the actual capitalisation. Map.put would replace the capitalised form,
but ConcurrentMap.putIfAbsent would preserve it.

Tom Hawtin
Back to top
Laie Techie
Guest





PostPosted: Tue Apr 10, 2007 12:58 pm    Post subject: Re: Case-insensitive collections (sets, maps, etc.) Reply with quote

On Wed, 14 Mar 2007 22:42:08 -0500, Matt wrote:

Quote:
I have to believe I'm not the first person to have this question.
However, I'm not having any luck finding an answer.

I'm using Java 1.5. Say I have the following:

Set<String> a = new HashSet<String>();
Map<String, Object> b = new TreeMap<String, Object>();

Assume somewhere these are populated.

When I check the set for a string using a.contains("bob"), the method is
checking using a case-sensitive search. Similarly, if I did
b.containsKey("tom"), it's case-sensitive.

Is it possible to get these methods to perform a comparison
case-insensitively?

TreeSet and TreeMap allow you to pass in a Comparator to the constructor.
This Comparator will dictate which objects should be considered equal.
For case-insensitivity, user String.CASE_INSENSITIVE_ORDER .

Set<String> set = new TreeSet<String> (String.CASE_INSENSITIVE_ORDER);
// now populate the set
Map<String, Object> map =
new TreeMap<String,Object>(String.CASE_INSENSITIVE_ORDER);
// now populate the map

Please note that neither TreeSet nor TreeMap are synchronized, so you need
to make them thread-safe yourself.

HTH,
La`ie Techie
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.