Is there a Class like HashMap, but...

B

ByteCoder

Is there a Class like HashMap, but which only stores the key.

Basicly, I want a Class which adds unique objects to it, but which
overwrites non-unique objects.

TIA,
 
S

Sudsy

ByteCoder said:
Is there a Class like HashMap, but which only stores the key.

Basicly, I want a Class which adds unique objects to it, but which
overwrites non-unique objects.

There's java.util.HashSet but it's backed by a HashMap so why not
just use a HashMap and set the value to null?
 
B

ByteCoder

ByteCoder said:
Is there a Class like HashMap, but which only stores the key.

Basicly, I want a Class which adds unique objects to it, but which
overwrites non-unique objects.

TIA,

I finally found it, it's the HashSet. :)
 
B

ByteCoder

ByteCoder said:
I finally found it, it's the HashSet. :)

One more thing: Does the HashSet has some sort of get method, so that I
don't have to use an Iterator if I just want to get one object?
Or wouldn't that be a problem if my HashSet has about 200 elements?
 
C

Chris Smith

ByteCoder said:
One more thing: Does the HashSet has some sort of get method, so that I
don't have to use an Iterator if I just want to get one object?
Or wouldn't that be a problem if my HashSet has about 200 elements?

No, it doesn't. There could never be a defined response for such a
method, since HashSet doesn't maintain any order for its elements. If
you want an arbitrary element, then set.iterator().next() is the easiest
way.

There should be minimal cost in creating an Iterator, and this cost
should not depend in any way on the size of the HashSet. I wouldn't
worry about it.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
B

ByteCoder

Sudsy said:
There's java.util.HashSet but it's backed by a HashMap so why not
just use a HashMap and set the value to null?

Or set the value to the same object as the key, or would that use too
much space (considering the HashMap might have ~200 elements)?

My object would have a few (<6) private variables, and a few methods
(again, <6).
 
B

ByteCoder

Chris said:
No, it doesn't. There could never be a defined response for such a
method, since HashSet doesn't maintain any order for its elements. If
you want an arbitrary element, then set.iterator().next() is the easiest
way.

There should be minimal cost in creating an Iterator, and this cost
should not depend in any way on the size of the HashSet. I wouldn't
worry about it.

Thanks. HashSet is still the most-likely solution to my problem.
 
C

Chris Smith

Sudsy said:
There's java.util.HashSet but it's backed by a HashMap so why not
just use a HashMap and set the value to null?

Correction: in the current version of the Sun implementation of the Java
API, HashSet is backed by a HashMap. That's certainly not specified
behavior.

One good reason not to use a HashMap and set the value to null is that
it seems almost like an intentional effort to obscure the purpose of the
code. If you want a set of something, you ought to be using a Set, not
a Map. It's beyond me why the implementation of that Set would matter
to you, except for performance reasons. I have yet to see any
indication that HashSet fails to perform well, or that a different
implementation would perform substantially better in any situation at
all.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

ByteCoder said:
Or set the value to the same object as the key, or would that use too
much space (considering the HashMap might have ~200 elements)?

1. 200 elements is tiny. Stop worrying about performance. When the
membership tops 10,000 you have my permission to lose some sleep over
performance, but only if performance is very important to your
application or you start seeing observable problems while testing.

2. It wouldn't use any more memory to set the value to the same object
as the key than it would to set the value to null. Not a single byte of
difference.

3. You should be kludging things like this anyway. Use HashSet. That's
what it's there for.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
B

ByteCoder

Chris said:
Correction: in the current version of the Sun implementation of the Java
API, HashSet is backed by a HashMap. That's certainly not specified
behavior.

One good reason not to use a HashMap and set the value to null is that
it seems almost like an intentional effort to obscure the purpose of the
code. If you want a set of something, you ought to be using a Set, not
a Map. It's beyond me why the implementation of that Set would matter
to you, except for performance reasons. I have yet to see any
indication that HashSet fails to perform well, or that a different
implementation would perform substantially better in any situation at
all.

It's indeed for performance reasons (this app will also run on citrix
clients, which means it will run on the server), but ease-of-use is
considered too. ;)
 
B

ByteCoder

Chris said:
1. 200 elements is tiny. Stop worrying about performance. When the
membership tops 10,000 you have my permission to lose some sleep over
performance, but only if performance is very important to your
application or you start seeing observable problems while testing.

2. It wouldn't use any more memory to set the value to the same object
as the key than it would to set the value to null. Not a single byte of
difference.

3. You should be kludging things like this anyway. Use HashSet. That's
what it's there for.

Right. HashSet it is! :)

Thanks for the explanation.
 
S

Scott Ellsworth

ByteCoder said:
One more thing: Does the HashSet has some sort of get method, so that I
don't have to use an Iterator if I just want to get one object?
Or wouldn't that be a problem if my HashSet has about 200 elements?

If you want to see whether a given object is in the set, use contains().
If you want to get all the objects in a set, use iterator, and if you
just want "any old object", use iterator().next().

Scott
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top