Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Matt - 15 Mar 2007 05:05 GMT
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:

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

would all yield the same result?

Thanks for your time.  Cheers,

Matt
--
Luc The Perverse - 15 Mar 2007 05:35 GMT
>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.
[quoted text clipped - 20 lines]
>
> Thanks for your time.  Cheers,

Just make a class called jerkString (because it's insensitive) and define
your own compare.   And then you can have it return it's string value when
asked.

--
LTP

:)
Lew - 15 Mar 2007 05:42 GMT
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?

> 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.
[quoted text clipped - 12 lines]
> 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
Lew - 15 Mar 2007 05:43 GMT
> Define a class wrapping String with a natural order based on
> case-insensitive comparison.

I should have said with equality based on ...

-- Lew
Lew - 15 Mar 2007 05:45 GMT
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?

> 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.
>
[quoted text clipped - 8 lines]
>
> Is it possible to get these methods to perform a comparison case-insensitively?  Thus, performing:

Define a class wrapping String with equality (and hashCode) based on
case-insensitive comparison. Use that as a base type in your Collections
instead of String.

-- Lew
Corona4456 - 15 Mar 2007 05:47 GMT
> 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.
[quoted text clipped - 23 lines]
> Matt
> --

Why not just force everything to be one or the other by using
String.toLowerCase()?
Lew - 15 Mar 2007 05:50 GMT
Matt <g...@yahoo.com> wrote:
>> I have to believe I'm not the first person to have this question.

Please do not multipost. It makes it hard to follow the thread, and why put
obstacles in people's path? If you really must reach multiple groups, cross-post.

-- Lew
Tom Hawtin - 15 Mar 2007 06:33 GMT
> Set<String> a = new HashSet<String>();
> Map<String, Object> b = new TreeMap<String, Object>();
[quoted text clipped - 4 lines]
> 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
Matt - 15 Mar 2007 14:58 GMT
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
--

>> Set<String> a = new HashSet<String>();
>> Map<String, Object> b = new TreeMap<String, Object>();
[quoted text clipped - 20 lines]
>
> Tom Hawtin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.