• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

StackOverflowError while getting map entry

 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the code below generates StackOverflowError?

output-
java.lang.StackOverflowError
 
Ranch Hand
Posts: 62
5
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Astha Sharma wrote:Why does the code below generates StackOverflowError?


This is a bit tricky. When you do a get on a hash-table based map, the map must first calculate the hashCode of the key. In the above code, the key is the HashMap m2. The hashCode of HashMap (see source code of AbstractMap) is the sum of hash codes from all Entries. The Entry calculates its hashCode using hashCode of key and value. But one key is the map m2 (put just before) .... and so calculates the hashCode of m2 using all entries .... and this goes theoretically to the infinite ..... causing somewhere a StackOverflowError.
 
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrea Binello wrote:

Astha Sharma wrote:Why does the code below generates StackOverflowError?


This is a bit tricky. When you do a get on a hash-table based map, the map must first calculate the hashCode of the key. In the above code, the key is the HashMap m2. The hashCode of HashMap (see source code of AbstractMap) is the sum of hash codes from all Entries. The Entry calculates its hashCode using hashCode of key and value. But one key is the map m2 (put just before) .... and so calculates the hashCode of m2 using all entries .... and this goes theoretically to the infinite ..... causing somewhere a StackOverflowError.



But there's nothing in m2, why would it infinitely calculate the hashcode for m2?
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for such nice explanation Andrea

Sidharth Khattri wrote:

Andrea Binello wrote:

Astha Sharma wrote:Why does the code below generates StackOverflowError?


This is a bit tricky. When you do a get on a hash-table based map, the map must first calculate the hashCode of the key. In the above code, the key is the HashMap m2. The hashCode of HashMap (see source code of AbstractMap) is the sum of hash codes from all Entries. The Entry calculates its hashCode using hashCode of key and value. But one key is the map m2 (put just before) .... and so calculates the hashCode of m2 using all entries .... and this goes theoretically to the infinite ..... causing somewhere a StackOverflowError.



But there's nothing in m2, why would it infinitely calculate the hashcode for m2?


For calculating hashcode of HashMap m2, it is needed to retrive hashcodes of all entries of HashMap m2. m2 is itself an entry inside m2. Thus process of retrieving hashcode for m2 will go on recursively.
 
Andrea Binello
Ranch Hand
Posts: 62
5
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sidharth Khattri wrote:But there's nothing in m2, why would it infinitely calculate the hashcode for m2?


There is one entry, added with:

m2.put(m2,1);

and the entry's key is a map, exactly that same map m2.

When you do m2.get(m2) the following things happen:

- calculation of hashCode for the key parameter (m2)
- m2 is a HashMap, its hashCode is calculated from all entries.
- one entry is m2-->1. The Entry calculates its hashCode XORing together hashCodes of key and value. But the key is a map (again m2).
- m2 is a HashMap, its hashCode is calculated from all entries.
- one entry is m2-->1. The Entry calculates its hashCode XORing together hashCodes of key and value. But the key is a map (again m2).
.......
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Astha Sharma wrote:Thanks for such nice explanation Andrea

Sidharth Khattri wrote:

Andrea Binello wrote:

Astha Sharma wrote:Why does the code below generates StackOverflowError?


This is a bit tricky. When you do a get on a hash-table based map, the map must first calculate the hashCode of the key. In the above code, the key is the HashMap m2. The hashCode of HashMap (see source code of AbstractMap) is the sum of hash codes from all Entries. The Entry calculates its hashCode using hashCode of key and value. But one key is the map m2 (put just before) .... and so calculates the hashCode of m2 using all entries .... and this goes theoretically to the infinite ..... causing somewhere a StackOverflowError.



But there's nothing in m2, why would it infinitely calculate the hashcode for m2?


For calculating hashcode of HashMap m2, it is needed to retrive hashcodes of all entries of HashMap m2. m2 is itself an entry inside m2. Thus process of retrieving hashcode for m2 will go on recursively.



Andrea Binello wrote:

Sidharth Khattri wrote:But there's nothing in m2, why would it infinitely calculate the hashcode for m2?


There is one entry, added with:

m2.put(m2,1);

and the entry's key is a map, exactly that same map m2.

When you do m2.get(m2) the following things happen:

- calculation of hashCode for the key parameter (m2)
- m2 is a HashMap, its hashCode is calculated from all entries.
- one entry is m2-->1. The Entry calculates its hashCode XORing together hashCodes of key and value. But the key is a map (again m2).
- m2 is a HashMap, its hashCode is calculated from all entries.
- one entry is m2-->1. The Entry calculates its hashCode XORing together hashCodes of key and value. But the key is a map (again m2).
.......



Thank you. Now I got it
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
:P
 
Author
Posts: 375
22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the implementation of method 'hashCode' in class 'AbstractMap', which is inherited by class 'HashMap':


If you are in doubt about the behaviour of a class or method, try to check its source code (if it is accessible).

With respect,
Mala
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mala
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic