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

M

Matt

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
--
 
L

Luc The Perverse

Matt said:
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,

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.
 
L

Lew

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.

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
 
L

Lew

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.

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 equality (and hashCode) based on
case-insensitive comparison. Use that as a base type in your Collections
instead of String.

-- Lew
 
C

Corona4456

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

Why not just force everything to be one or the other by using
String.toLowerCase()?
 
L

Lew

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
 
T

Tom Hawtin

Matt said:
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
 
M

Matt

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top