Re: Seeming Non-determinism in Java Programs
Stephen Denne <[email protected]> Mon, 7 Apr 2008 16:01:38 +1200
| Newsgroups | gmane.comp.windows.devel.java.advanced |
|---|---|
| Message-ID | <[email protected]> |
Randall R Schulz wrote:
> On Sunday 06 April 2008 17:56, Stephen Denne wrote:
> > Randall R Schulz wrote:
> > > ...
> > >
> > >
> > > Has anyone else seen this sort of phenomenon? If so, how have
> > > you dealt with it?
> >
> > I have encountered problems just like this.
> > I can't recall the cause at the moment, I could probably figure it
> > out based on my solution: I solved it by changing my usage
> of HashMap
> > (or indirect use of HashMap within HashSet) to a LinkedHashMap. That
> > way when iterating over the entries, the returned order is
> always the
> > same. The order wasn't important to me, but variations in the order
> > where observed, and variations resulted in different results from
> > various heuristic algorithms.
> >
> > I think the reasons where due to Object.hashCode()
> > Important bit from the Javadocs:
> > (This is typically implemented by converting the internal address of
> > the object into an integer, but this implementation technique is not
> > required by the JavaTM programming language.)
>
> But as far as I know (and certainly as I intended), no class whose
> instances are stored in hashed structures rely on the default
> implementation of hashCode(). That implementation is only congruent
> with the default implementation of equals() as instance identity. I
> never overload one of those without overloading the other, so this
> should not be an explanation for what I'm seeing (thus leading to my
> perplexity).
Agreed.
I'd be inclined to explore the "as far as I know".
Have you tried a profiler, or instrumented your code to track all usages of Object.hashCode(), to rule this out?
For example, if you have a HashSet of HashMaps... HashMap's hashCode(), inherited from AbstractMap, sums the hashes of the entryset Entry.hashCode() which includes the hash of the values, not just the hash of the keys.
Regards,
Stephen Denne.
Disclaimer:
At the Datamail Group we value team commitment, respect, achievement, customer focus, and courage. This email with any attachments is confidential and may be subject to legal privilege. If it is not intended for you please advise by reply immediately, destroy it and do not copy, disclose or use it in any way.
__________________________________________________________________
This email has been scanned by the DMZGlobal Business Quality
Electronic Messaging Suite.
Please see http://www.dmzglobal.com/dmzmessaging.htm for details.
__________________________________________________________________
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com