12/22/2023 0 Comments Deep copy hashmap java![]() To achieve a deep copy, extra logic must be added that explicitly calls all contained objects' clone() methods, which in turn call their contained objects' clone() methods, and so on. The default clone() method, inherited from Object, makes a shallow copy of the object. Each object is responsible for cloning itself via its clone() method. If either of these contained objects themselves contain objects, then, in a deep copy, those objects are copied as well, and so on until the entire graph is traversed and copied. Not only has obj1 been copied, but the objects contained within it have been copied as well. Figure 3 shows obj1 after a deep copy has been performed on it. After a shallow copy of obj1Ī deep copy occurs when an object is copied along with the objects to which it refers. ![]() If a shallow copy is performed on obj1, then it is copied but its contained objects are not, as shown in Figure 2. To illustrate, Figure 1 shows an object, obj1, that contains two objects, containedObj1 and containedObj2. To summarize briefly here, a shallow copy occurs when an object is copied without its contained objects. ![]() In a previous JavaWorld article, " How to avoid traps and correctly override methods from ," Mark Roulo explains how to clone objects as well as how to achieve shallow copying instead of deep copying. In order to understand what a deep copy is, let's first look at the concept of shallow copying. This article describes a simple technique to use in place of this time-consuming conventional deep copy. Traditionally, each class in the object must be individually inspected and edited to implement the Cloneable interface and override its clone() method in order to make a deep copy of itself as well as its contained objects. Implementing a deep copy of an object can be a learning experience - you learn that you don't want to do it! If the object in question refers to other complex objects, which in turn refer to others, then this task can be daunting indeed. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |