A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a map.
Does this mean that it is sintactically allowed (it will compile) but semantically it won't work because of equals and hashCode methods are not well defined?
Yes, that is correct. Compiler cannot know what exact object map variable points to at run time and it cannot execute the code of put method to determine that putting map as the key is not permitted, and that is why it permits the call at compile time.
If you like our products and services, please help us by posting your review here.
Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a map.