Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > HashSet with complicated objects

Reply
Thread Tools

HashSet with complicated objects

 
 
Mike
Guest
Posts: n/a
 
      11-20-2004
Hey!

I'm new to Java and want to try to do things properly, so would be
grateful for guidance on the following.

I have a HashSet of fairly complicated objects, and don't want to
instantiate a new one to use the "contains" method to see if the object
exists in the HashSet. Nonetheless, I need to find out if the nascent
object is in the Set (specifically, if I did create a new instance, would
it be the same as one already there).

So, my question is:

1. Is there an idiomatic way to do this, or
2. Has the entire fleet left with me standing on the dock?

Many thanks.

Regards,

Mike
 
Reply With Quote
 
 
 
 
William Brogden
Guest
Posts: n/a
 
      11-20-2004
On Sat, 20 Nov 2004 02:33:57 GMT, Mike <>
wrote:

> Hey!
>
> I'm new to Java and want to try to do things properly, so would be
> grateful for guidance on the following.
>
> I have a HashSet of fairly complicated objects, and don't want to
> instantiate a new one to use the "contains" method to see if the object
> exists in the HashSet. Nonetheless, I need to find out if the nascent
> object is in the Set (specifically, if I did create a new instance, would
> it be the same as one already there).
>
> So, my question is:
>
> 1. Is there an idiomatic way to do this, or
> 2. Has the entire fleet left with me standing on the dock?


Recall that contains() will use only hashCode() and equals() methods
so it all depends on how your objects implement those methods.
If there is a way to make a "lightweight" instance of one of
these objects that will produce the same hashcode and equals
results as a "heavyweight" object, you can use it as a probe
for the HashSet.

Bill
 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      11-20-2004
"Mike" <> wrote in message
news...
> Hey!
>
> I'm new to Java and want to try to do things properly, so would be
> grateful for guidance on the following.
>
> I have a HashSet of fairly complicated objects, and don't want to
> instantiate a new one to use the "contains" method to see if the object
> exists in the HashSet. Nonetheless, I need to find out if the nascent
> object is in the Set (specifically, if I did create a new instance, would
> it be the same as one already there).
>
> So, my question is:
>
> 1. Is there an idiomatic way to do this, or
> 2. Has the entire fleet left with me standing on the dock?
>
> Many thanks.
>
> Regards,
>
> Mike


The fact that you are talking about a HashSet implies that you aren't doing
things the right way.
Specifically, you should be handling types with an interface reference, such
as a java.util.Set.

Here is an example:
Set<?> s = new HashSet<?>();

So now you are talking about a Set - the underlying implementation being
less relevant, since implementation details are always less relevant than
the exposed interface when things are done the "correct way".
The contains() method of Set performs in a very specific way. If you are
storing your own types, it might be worthwhile to override the Object.equals
and Object.hashCode methods so that Set.contains() performs as you desire
(this is also the "correct approach" that you are seeking). Now, I've seen
these methods overridden hundreds of times - but only 0.1% have I seen it
done correctly. The API specification is very precise about the general
contracts of these two methods - ensure that you meet that contract or the
consequences are dire (and often under-estimate).

Here are some links of interest:

The two Object method contracts (which are interrelated - not independant of
each other):
http://java.sun.com/j2se/1.5.0/docs/...va.lang.Object)
http://java.sun.com/j2se/1.5.0/docs/...html#hashCode()

Set.contains()
http://java.sun.com/j2se/1.5.0/docs/...va.lang.Object)

These third-party (authored by myself) data types will ensure that you have
met the general contracts for your type:
http://www.xdweb.net/~dibblego/junit...actTester.html
http://www.xdweb.net/~dibblego/junit...erFactory.html
http://www.xdweb.net/~dibblego/junit...actTester.html
http://www.xdweb.net/~dibblego/junit...erFactory.html

--
Tony Morris
http://xdweb.net/~dibblego/



 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      11-20-2004
On Sat, 20 Nov 2004 03:34:26 +0000, Tony Morris wrote:

> "Mike" <> wrote in message
> news...
>> Hey!
>> [dumb question was here]


>> Many thanks.
>>
>> Regards,
>>
>> Mike

>
> The fact that you are talking about a HashSet implies that you aren't doing
> things the right way.
> Specifically, you should be handling types with an interface reference, such
> as a java.util.Set.
>

Yes, sorry, sloppy language. I hate it when I do that. The Set is
declared as a Set but instantiated as a HashSet, as you have shown below.

> Here is an example:
> Set<?> s = new HashSet<?>();
>
> So now you are talking about a Set - the underlying implementation being
> [... now-obvious-seeming explanation deleted for brevity ...]
> that you meet that contract or the consequences are dire (and often
> under-estimate).
>


(sound of penny, probably dislodged by whacking side of head with palm
of hand, dropping into the mechanism)

Yah, gotcha! D'oh.

> Here are some links of interest:
> [useful links elided]


Excellent, thank you!

Thanks also to Bill Brogden for the whack the other way.

Regards,

Mike
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HashSet keeps all nonidentical equal objects in memory Frederik Java 5 07-20-2011 04:31 PM
HashSet performance zero Java 14 11-14-2005 01:12 AM
HashSet is strange Anony! Java 8 08-29-2004 06:07 PM
Can python control complicated classes/objects written in C++ Bo Peng Python 7 06-05-2004 05:40 PM
Re: HashSet doesn't work correctly!!?? La'ie Techie Java 0 09-26-2003 01:22 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57