[concurrency-interest] Reference/IdentityMap toString

Jason Mehrens jason_mehrens at hotmail.com
Wed Aug 18 18:12:52 EDT 2010

> I must understand I still can't see any use cases for this.
> Why would you want to print something else but {1=1} in your 
> IdentityHashMap but not in your HashMap.
Because identity/reference maps view entries by what they reference and not the contents of what is referenced.  It's a code illusion to print the contents of each entry (my initial point).  Plus the other reasons listed in the last post.
> And why isn't CCHM.toString 
> safe from stack overflow, unless someone is dumb enough to insert the 
> map into itself.
You don't necessarily have to be dumb, you could be evil.  Back to the point, identity maps are used in object graph traversals to track what has been visited.  What happens when the tracker map tracks itself? Equals and hashCode work fine but lookout toString.  There is more than one way to poison the map:
m.put(K, Collections.nCopies(Integer.MAX_VALUE, Integer.MAX_VALUE));

> Besides, you don't want to ever compare string representations of Maps. 
> Maps are free to list the elements in any order they want.
Maps, entries, and collections are free to create whatever toString they want, it's not defined in the corresponding interface contracts at all.  They don't have to list any entries.
Comparing the result of the representations of the maps is just to prove that it is a code illusion.  One way or another, map.toString ends up in a log or in a debug watch and that is when the illusion begins.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20100818/8d2a213f/attachment.html>

More information about the Concurrency-interest mailing list