Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinkedHashMap<String,Object>.clone();

Tags:

java

clone

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

like image 747
blippy Avatar asked Jan 13 '10 17:01

blippy


2 Answers

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.
like image 187
Aaron Digulla Avatar answered Sep 25 '22 22:09

Aaron Digulla


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.)

like image 25
Jon Skeet Avatar answered Sep 24 '22 22:09

Jon Skeet