When a key-value pair is added to a HashMap - a hashCode for the input key is generated, while the value can be virtually anything, even null. An ArrayList hashCode is calculated based on the hashCodes of all of its elements. Consequently, adding the list first time, will generate no error, because the hashMap is empty, and the list hashCode can be easily calculated. The second addition, however, will generate a StackOverflowError (the list hashCode needs to be calculated based on HashMap hashCode, which needs to be calculated based on the list hashCode, etc).
Войдите чтобы поставить Нравится
Войдите чтобы прокомментировать