[concurrency-interest] Reference/IdentityMap toString

Kasper Nielsen kasper at kav.dk
Thu Aug 19 02:49:33 EDT 2010

On 19/08/10 00.12, Jason Mehrens wrote:
> Kasper,
>  > 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.
> Regards,
> Jason

You still haven't come up with any convincing use case. I've never seen 
any bugs related to this behavior. And if you want a custom toString 
method for CCHM you can just use the CCHM.entrySet().iterator to iterate 
through all elements and print whatever you want. Requiring people to 
implement a toString(Object) method whenever they implement the 
Equivalence interface is just a waste of peoples time IMO.


More information about the Concurrency-interest mailing list