aspose file tools
The moose likes Java in General and the fly likes StackOverflowError using hashCode() in HashMap in recursive method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "StackOverflowError using hashCode() in HashMap in recursive method" Watch "StackOverflowError using hashCode() in HashMap in recursive method" New topic
Author

StackOverflowError using hashCode() in HashMap in recursive method

Edward Martinez
Greenhorn

Joined: May 08, 2009
Posts: 2
Hi everyone,
OK, I'm really stumped here. I'm getting a StackOverflowError, which is occurring on the first line of my overridden hashCode() method of my POJO. hashCode() is being called from the get() method of HashMap, which is used in a recursive method. The map is not self-referential. Here's a snippet of hashCode().



This is generated by Eclipse.

Here's what the recursion looks like:



Any ideas anyone has would be greatly appreciated.

Thanks,

Eddie
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Well, apparently never happens and you keep recursively calling the method. Have you overridden the equals(Object) method as well? Can you post it here?
Edward Martinez
Greenhorn

Joined: May 08, 2009
Posts: 2
Hi Piet,
Thanks for your response. Sorry it took me so long to get back to you.

Here are the attributes of my class:



Here's my equals() method, which was generated by Eclipse:


I may have found a workaround for this. I started using a String as the key in the HashMap, rather than the complex type, and the infinite recursion has disappeared, so far. I'm going to let it soak for the rest of this week and see how it does.

Thanks for your help,

Eddie
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

That equals method looks really ugly. With just a little utility method you can make it so much easier to read:


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Rob Prime wrote:... With just a little utility method you can make it so much easier to read:
...


Indeed!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
You should never actually look at an automatically generated method. Not unless you have a very strong stomach
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: StackOverflowError using hashCode() in HashMap in recursive method
 
Similar Threads
tranfer map values from one to another
Confusion with hashCode() and equals() method
does gc still have a chance to do its job if i kill the java process
can we write junit testcase for any java class?
Need help for refreshing the page