Re: Seeming Non-determinism in Java Programs
Randall R Schulz <[email protected]> Sun, 6 Apr 2008 18:04:22 -0700
| Newsgroups | gmane.comp.windows.devel.java.advanced |
|---|---|
| Message-ID | <[email protected]> |
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). > The internal address varied between runs. > > Regards, > Stephen Denne. Randall Schulz =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com