How hard can it be?

Hi,

Do you know that feeling when you are doing something and you just have a really good sense that you are doing things way too complicated?

I have a HashSet where I have put a lot of counter objects. Once in a while I would like to have a look in the HashSet, find one of the objects and run its "thisObjectWasFoundEvenOneMoreTime()" method. My problem is that HashSet does not have any "get(Object o)" method. The closest method I found was "contains" which only tells me whether or the object is present in the HashSet. I cannot use it to run the "thisObjectWasFoundEvenOneMoreTime()" method.

I know that I could probably try to make my own version of HashMap, but I tried to extend it and there are quite a few non-visible methods and variables in it so I would basically have to do some copying and pasting of the source code of HashMap (which surely is not the right way). How can this problem be solved?

Regards,

David

[964 byte] By [hulagutten2a] at [2007-11-27 4:50:18]
# 1
What are you talking about? HashSet or HashMap? Putting counters in a set seems quite pointless to me, hence I assume you are having some key object and a counter? In that case, HashMap will do perfectly fine (as, of course, it has a get(Object) method).
stefan.schulza at 2007-7-12 10:03:36 > top of Java-index,Core,Core APIs...
# 2

How do you look inside a HashSet and "find" something? The point of a (Hash)Set is to provide "A collection that contains no duplicate elements."

There is no "finding" in a HashSet. You can get at the items in a Set by iterating over it. This process will return each item in the set one by one.

If you want to "find" or "retrieve" an element based on some other information, you should indeed use a Map, which uses a key to get at a value, for example an ID (in the form of a String or Integer) to get at an Employee object.

Herko_ter_Horsta at 2007-7-12 10:03:36 > top of Java-index,Core,Core APIs...
# 3

So you have an object; you can find out if the HashSet contains that object; but then you can't use the object? That sounds nonsensical. Let me rephrase.

You have a HashSet into which you inserted object A. You now have another object, let's call it object B. And B.equals(A) and B.hashCode()==A.hashCode() both return true, so A and B are in some sense the same object. But you can't use B to get A out of the HashSet. Is that a more accurate description?

DrClapa at 2007-7-12 10:03:36 > top of Java-index,Core,Core APIs...
# 4

I have taken over a program that uses a HashMap for a similar problem (if I've understood yours correctly; maybe my head is just so filled-up with my own problem that I can't see yours is different). I agree with Stefan, a Map is fine.

In my case, the Map just maps String objects to String objects, the latter holding a number, e.g., "2", which is really stupid. To increase a counter, I need to parse the number, increment, convert back to String and store the new String into the Map. I think you inpired me to build a Counter class to use instead if I find time to change it. My Counter class would simply hold an int and have an incr method to add 1 to the int. Then I will map String objects to Counter objects and be happy :-)

OleVVa at 2007-7-12 10:03:36 > top of Java-index,Core,Core APIs...
# 5

> Do you know that feeling when you are doing something

> and you just have a really good sense that you are

> doing things way too complicated?

Yes, i know that feeling. Last night, i was trying to make my own rocket to go on Mars. I was

with my glue and my scissors and i thought that i shouldn't glue one part to the other because

it will be to complicated after to install the reactor...

suparenoa at 2007-7-12 10:03:36 > top of Java-index,Core,Core APIs...
# 6
Thanks DrClap, that is an accurate description. How do I solve such a problem?
hulagutten2a at 2007-7-12 10:03:36 > top of Java-index,Core,Core APIs...
# 7
Thanks DrClap. You have rephrased it accurately. How do I solve such a problem?
hulagutten2a at 2007-7-12 10:03:36 > top of Java-index,Core,Core APIs...
# 8
I actually found a solution now. I use a HashMap where both the key and value reference to the same object.
hulagutten2a at 2007-7-12 10:03:36 > top of Java-index,Core,Core APIs...
# 9
I'm using the same solution for now, but probably like you, I don't like it. I find it odd that Set doesn't support such a get function. Why be forced to have the unnecessary overhead of a map to perform such a basic function? Perhaps we'll see such a get method in a future version,
me.ina at 2007-7-12 10:03:36 > top of Java-index,Core,Core APIs...
# 10
What unnecessary overhead? Are you aware that HashSet is implemented with a HashMap? so the overhead is actually the other way around?I would have said the correct solution is a HashMap<YourObject, Integer> myself but YMMV.
ejpa at 2007-7-12 10:03:36 > top of Java-index,Core,Core APIs...