JDK-6693509 : Deep Cloning for Map
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 7
  • Priority: P5
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2008-04-24
  • Updated: 2021-03-03
  • Resolved: 2010-11-21
Description
A DESCRIPTION OF THE REQUEST :
Map is not able to do the deep cloning, while calling the clone() method on it.. It just uses the reference copy, not creating the another cloneed object.

JUSTIFICATION :
This feature should be there because, if i need another map very similar to the same map and i want to retain the first map as static and changing the cloned map, the actual map should not be change.
  To do this, clone() method in the collection object need to be changed to perfrom deep cloning insteed of just copying the same references. Either modify clone() method or need to introduce another method to do this stuffs.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The Actual map should not changed, when ever i am changing the cloned map.
ACTUAL -
The Actual map is also getting changed, when ever i am changing the cloned map.

Comments
PUBLIC COMMENTS Deep cloning is intentionally not provided by the Collections classes. If deep cloning is desired it should be performed by the application. Consider the following typical deep clone implementation: Map newMap = new Map(oldMap.size()); for(Map.Entry entry : oldMap.entrySet()) { newMap.put(entry.getKey(), entry.getValue().clone()); } Perhaps the keys should have been cloned as well? The semantics required depend too much on the application and the characteristics of the keys and values for a one size fits all deepClone to be practical.
21-11-2010