J.IV · Java 25 practice question
A developer accidentally inserts a Map into itself as a value. According to the Java SE 25 Map specification, what may happen when calling equals(), hashCode(), toString(), or clone() on that map?
Check answer and explanation
Correct answer: The operation may fail with an exception because most implementations do not handle self-referential recursive traversal.
Explanation
Recursive traversal methods such as clone(), equals(), hashCode(), and toString() may fail with an exception on self-referential maps, as most current implementations do not handle that scenario.
Common trap
While some implementations like AbstractMap.toString() guard against self-reference, the Map specification states that most implementations do not handle self-referential scenarios in general. Map.put() does not perform cycle detection, so self-referential insertion itself does not fail with IllegalArgumentException.
Source: Java SE 25 API: Map
Checked against the cited text on 2026-11-25.