This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashMap StackOverflowError problem - please explain me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashMap StackOverflowError problem - please explain me" Watch "HashMap StackOverflowError problem - please explain me" New topic
Author

HashMap StackOverflowError problem - please explain me

Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

Hi,

Predict the result !!! and please explain me

HashMap map= new HashMap();
map.put(map,"abdul");

System.out.println(map.get(map));

Regards,

Abdul Mohsin
[ May 18, 2007: Message edited by: Abdul Mohsin ]

Regards, Abdul Mohsin
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
See the links:
One
Second



Thanks,


cmbhatt
Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

Hi Chandra,

thanks for these links but my question is why the above code is giving StackOverflowError

Regards,

Abdul Mohsin
Mirko Bonasorte
Ranch Hand

Joined: May 14, 2007
Posts: 244
The problem is the AbstractMap.hashCode(), which uses the hashCode keys to build it. If the keys contains itself, you have an infinite loop.


SCJP<br />SCWCD 1.4 Upgrade (Remember: me stupid)<br />SCWCD 1.4<br /><a href="http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html" target="_blank" rel="nofollow">SCBCD 5.0</a><br /><a href="http://www.enthuware.com" target="_blank" rel="nofollow">SCBCD 5.0 mock exam</a> <br /> <br />SCEA 5 Part1: Preparing...
Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

Hi,

I just gone through the AbstractMap java doc ,but still its not clear why its going in infinite loop.

Regards,

Abdul Mohsin
 
wood burning stoves
 
subject: HashMap StackOverflowError problem - please explain me
 
Similar Threads
Question regex
ClassCast exception
Doubt pertaining to '==' operator
Collections
Guess the answer and please explain