Astha - OCPJP 6 (90%)
Astha Sharma wrote:Why does the code below generates StackOverflowError?
Andrea, andbin.dev — SCJP 5 (91%) – SCWCD 5 (94%)
java.util.function Interfaces Cheat Sheet — Java Versions Cheat Sheet
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.
OCPJP 6 - 96%
Currently working on Scala at Knoldus Software
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?
Astha - OCPJP 6 (90%)
Sidharth Khattri wrote:But there's nothing in m2, why would it infinitely calculate the hashcode for m2?
Andrea, andbin.dev — SCJP 5 (91%) – SCWCD 5 (94%)
java.util.function Interfaces Cheat Sheet — Java Versions Cheat Sheet
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).
.......
OCPJP 6 - 96%
Currently working on Scala at Knoldus Software
OCPJP 6 - 96%
Currently working on Scala at Knoldus Software
Prepare for 'OCA Java SE 8 certification' with eJavaGuru.com
Author of Manning's OCA Java SE 8 bookProgrammer I Certification Guide, OCA Java SE 7 Programmer I Certification Guide and OCP Java SE 7 Programmer II Certification Guide
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|