By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

How to check if a string exists in a set of strings?

P: n/a
Hi All,

What is the fastest way to check the existence of a string in a set of
strings?

I have thought of putting the set of strings in a HashMap and then us
containsKey() to do the check but I don't like because this is the
objective of HashMap.

What else do you recommend?

Thanks in advance.

Jul 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Andrew Thompson wrote:
On Fri, 12 Mar 2004 03:05:46 GMT, someone wrote:

What is the fastest way to check the existence of a string in a set of
strings?

HashMap

I have thought of putting the set of strings in a HashMap and then us
containsKey() to do the check but I don't like because this is the
objective of HashMap.

?!?! So, you would not think to
use Applet class for an Applet,
because that is the objective of
Applet?


Sorry, I miss-typed it. I meant NOT the objective because in my case I
don't key-value pairs, I have only keys.
Reminds me of 'Flying High'
"But no ..wait. That's _exactly_
what they'll be expecting."

What else do you recommend?

HashMap, ..and posting information
or questions that makes sense.


Jul 17 '05 #2

P: n/a
someone wrote:

Sorry, I miss-typed it. I meant NOT the objective because in my case I
don't key-value pairs, I have only keys.


Then, clearly, use HashSet.
Jul 17 '05 #3

P: n/a
Andrew Thompson wrote:
On Fri, 12 Mar 2004 03:28:12 GMT, someone wrote:

Andrew Thompson wrote:
On Fri, 12 Mar 2004 03:05:46 GMT, someone wrote:
..
What is the fastest way to check the existence of a string in a set of
strings?
..
I have thought of putting the set of strings in a HashMap and then us
containsKey() to do the check but I don't like because this is the
objective of HashMap.

...
Sorry, I miss-typed it. I meant NOT the objective..

Ahaaa! Thanks for clarifying.
[ As you may have guessed, that
read quite ..oddly. ]

I expect Carl's answer solved the problem?


Yes, of course!
[ defunct group c.l.j deleted *again* ]


Jul 17 '05 #4

P: n/a
Carl Howells wrote:
someone wrote:

Sorry, I miss-typed it. I meant NOT the objective because in my case I
don't key-value pairs, I have only keys.

Then, clearly, use HashSet.


Carl, thanks a lot!!!

Jul 17 '05 #5

P: n/a
someone wrote:
Sorry, I miss-typed it. I meant NOT the objective because in my case
I don't key-value pairs, I have only keys.


Then, clearly, use HashSet.

Carl, thanks a lot!!!


Although HashSet is in fact nothing but a wrapper around a HashMap that hides the
values functionality.

Jul 17 '05 #6

P: n/a
Michael Borgwardt wrote:
someone wrote:
Sorry, I miss-typed it. I meant NOT the objective because in my case
I don't key-value pairs, I have only keys.


Then, clearly, use HashSet.


Carl, thanks a lot!!!

Although HashSet is in fact nothing but a wrapper around a HashMap that
hides the
values functionality.


What do you suggest?

Jul 17 '05 #7

P: n/a
someone wrote:
Although HashSet is in fact nothing but a wrapper around a HashMap
that hides the values functionality.

What do you suggest?


Oh, using HashSet is fine, it makes the intent clearer. I just wanted
to point out that you still end up using a HashMap underneath (with
null values).
Jul 17 '05 #8

P: n/a
Michael Borgwardt wrote:
someone wrote:
Although HashSet is in fact nothing but a wrapper around a HashMap
that hides the values functionality.


What do you suggest?

Oh, using HashSet is fine, it makes the intent clearer. I just wanted
to point out that you still end up using a HashMap underneath (with
null values).


In fact, I noticed a performance difference between HashMap and HashSet
for my case, HashSet was faster.

Jul 17 '05 #9

P: n/a

"someone" <so*****@somewhere.com> wrote in message
news:40**************@somewhere.com...
Michael Borgwardt wrote:
someone wrote:
Although HashSet is in fact nothing but a wrapper around a HashMap
that hides the values functionality.

What do you suggest?

Oh, using HashSet is fine, it makes the intent clearer. I just wanted
to point out that you still end up using a HashMap underneath (with
null values).


In fact, I noticed a performance difference between HashMap and HashSet
for my case, HashSet was faster.


That is very unlikely since HashSet does no more than forward calls to a
private HashMap.

Silvio Bierman
Jul 17 '05 #10

P: n/a
Silvio Bierman wrote:
"someone" <so*****@somewhere.com> wrote in message
news:40**************@somewhere.com...
Michael Borgwardt wrote:
someone wrote:
>Although HashSet is in fact nothing but a wrapper around a HashMap
>that hides the values functionality.

What do you suggest?
Oh, using HashSet is fine, it makes the intent clearer. I just wanted
to point out that you still end up using a HashMap underneath (with
null values).


In fact, I noticed a performance difference between HashMap and HashSet
for my case, HashSet was faster.

That is very unlikely since HashSet does no more than forward calls to a
private HashMap.


Maybe this is due to saving the overhead of assigning the value field of
the key-value pair!?

Jul 17 '05 #11

P: n/a

"someone" <so*****@somewhere.com> wrote in message
news:40**************@somewhere.com...
Silvio Bierman wrote:
"someone" <so*****@somewhere.com> wrote in message
news:40**************@somewhere.com...
Michael Borgwardt wrote:

someone wrote:
>>Although HashSet is in fact nothing but a wrapper around a HashMap
>>that hides the values functionality.
>
>
>
>What do you suggest?
Oh, using HashSet is fine, it makes the intent clearer. I just wanted
to point out that you still end up using a HashMap underneath (with
null values).

In fact, I noticed a performance difference between HashMap and HashSet
for my case, HashSet was faster.

That is very unlikely since HashSet does no more than forward calls to a
private HashMap.


Maybe this is due to saving the overhead of assigning the value field of
the key-value pair!?


I think you are missing the point here. HashSet does not save that overhead
since it DOES assign a value field for every key you insert (with value
null). So if you compare HashSet to using HashMap with null-values for each
key yourself HashSet is totally equivalent except for adding the overhead of
forwarding the call to the internal HashMap.

Note that this is a minor detail and using HashSet for this situation is
always better than using HashMap with null-values since it makes your intent
explicit. The performance penalty induced by the HashSet implementation is
negligible.

Regards,

Silvio Bierman
Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.