views:

159

answers:

2

does the above command produce a deep copy of a LinkedHashMap's elements?

+1  A: 

In Java, clone() is almost always shallow. This is for two reasons:

  1. Performance
  2. Not every object defines a working clone() method, so deep copying isn't always possible.
Aaron Digulla
Unless overridden to do something else.
Steve Kuo
+1  A: 

LinkedHashMap derives from HashMap, which specifies this for the clone() method:

Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.

(So no, it's a shallow clone rather than deep. Not that it really matters for the strings.)

Jon Skeet