I have a class containing Map whose key is string and value is object of another class.
Class Employee {
String name;
}
Class Company {
Map<String, Employee> m =new HashMap<String, Employee>();
}
I have two Company objects obj1 and obj2, where I want to copy the map from one object to another object.
If I do any of the below things,
obj1 = obj2 or
obj1.setM(obj2.getM()) // assume we have setters and getters
now if I manipulate obj1’s map, making the any of the value to point to different object of Employee for same Company object that obj2 has, where obj2’s map is also getting affected.
Is there any way (other than copying key and value to new map and assigning it to object), where we can copy the map contents to another object’s map such that manipulating one map doesnot affect other map. Any suggestions are really appreciated.
Try: