What is the best of cloning a LinkedHasMap in Java?
I already tried:
Map<String, Object> clonedMap = new LinkedHashMap<String, Object>(originalMap); But that didn't work.
The easiest way to get a deep copy is to serialize the map and then deserialize it. The faster way is to go thought the whole map, clone each key/value and put it to a new map.
In case you need a shallow copy - your snippet does that correctly.
new Map(map)makes a "shallow" copy - the references are the same. If you mean a "deep" copy (where all keys and values are also cloned) the answer depends entirely on the implementation of the the key and value classes.