[concurrency-interest] Reference/IdentityMap toString

Kasper Nielsen kasper at kav.dk
Wed Aug 18 15:40:54 EDT 2010


On 18/08/10 20.14, Jason Mehrens wrote:
> Kasper,
>
> Debuging and logging are key areas. Take the following:
>
> //=========================================
> Map<Integer, Integer> eq = new HashMap<Integer, Integer>();
> eq.put(new Integer(1), new Integer(1));
>
> Map<Integer, Integer> id = new IdentityHashMap<Integer, Integer>();
> id.put(new Integer(1), new Integer(1));
>
> System.out.println(id.equals(eq));
> System.out.println(id.toString().equals(eq.toString()));
> //=========================================
>
> I think it would be ideal if this printed false, false and not false,
> true. Changing the toString implementation to use identity strings would
> mean that CCHM.toString would be safe from stack overflow, would not
> unknowingly acquire an internal lock, and would have a more predictable
> runtime (Map<K, Collection<E>>. CCHM hashCode and equals enjoy those
> benefits when in identity mode so why shouldn't CCHM.toString?

Jason,

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. And why isn't CCHM.toString 
safe from stack overflow, unless someone is dumb enough to insert the 
map into itself.

Besides, you don't want to ever compare string representations of Maps. 
Maps are free to list the elements in any order they want.

Cheers
   Kasper


More information about the Concurrency-interest mailing list