0

Consider, that I have a driver function foo() which is defined as follows:

void foo()
{
    Map<Integer,Character> map = new HashMap<Integer,Character>() ;
    bar(map)
    //operations on map
}
void bar(Map<Integer,Character> map)
{
    Map<Integer,Character> map2 = new HashMap<Integer,Character>(map) ;
    //operations over map2
}

Now, according to what I know, the operations on map2 should change the original map, and therefore operations on map inside foo() would be on a different version of map than the original one. Reason for my belief:

  1. According to this post, we can say that we're making a shallow copy of the Hash Map.
  2. In this documentation, in which it is clearly said that:

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.

Therefore, since we're making a shallow copy, operations over the copy will ultimately change the passed map, and all the changes over this passed map are reflected over the original map, due to point 2.

But I've got a contradictory example that questions my understanding. So, is my thinking correct? Or there is something else to it?

Mooncrater
  • 4,146
  • 4
  • 33
  • 62
  • 2
    I'm really not following you. copy means the same thing as in English. If you make a copy of a document, and modify the copy, the original document stays unmodified. That's the whole point of a copy. So no, changes to map2 won't make any chnge to map. You could experiment and test that easily, BTW. – JB Nizet Oct 17 '17 at 16:54
  • 4
    Shallow or deep copy doesn't matter here: the map contains only immutable values. – Andy Turner Oct 17 '17 at 16:55
  • 1
    Define what you mean by "change the passed map'. If you mean its set of key/values, then they wont change for `map2` when you add or remove a key to/from `map`. If you mean the actual state of key or value objects when you change their state (via set methods for example), then they will be reflected in both maps because both maps will reference the same key/value objects. – tsolakp Oct 17 '17 at 17:24

1 Answers1

-2

Initializing a hashmap from another map will not lead to shallow copy. New map object will have its own datatables and thus deleting or adding entries won't affect both maps. However, key and value objects in both maps will be same and thus any operation on key and value objects will be visible in both provided those objects are not immutable and not string objects

Sunil Singhal
  • 593
  • 3
  • 11
  • 1
    What you're describing is precisely the definition of a shallow copy. So yes, the copy constructor creates a shallow copy. – JB Nizet Oct 17 '17 at 17:00
  • No @JBNizet . Hashmap internally maintains datatables which contains keys and values objects. Datatables are not shallow copied. – Sunil Singhal Oct 17 '17 at 17:04
  • 1
    "Initializing a hashmap from another map will not lead to shallow copy." Absolutely wrong. Don't confuse yourself by bringing the internal structure of a `HashMap` here (which is not called a "datatable"). – Kayaman Oct 17 '17 at 17:17
  • @Kayaman Hashmap source code is available here at http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/HashMap.java#HashMap.putAllForCreate%28java.util.Map%29. And if go through the overloaded constructor code, you will see that internal datatables is prepared a fresh and not a shallow copy. So any modification to datatables will not be reflected in original map. However, same is not true for keys and values objects – Sunil Singhal Oct 17 '17 at 17:33
  • 1
    It's still a shallow copy and not a deep copy. If the reference to the internal structure were copied, you wouldn't create a copy at all. They would essentially be the same map. You seem to be confused about the difference between a shallow copy and deep copy. – Kayaman Oct 17 '17 at 17:39
  • That's an implementation detail. What the term shallow copy means is that a copy of the collection/data structure is made, but not a copy of the elements in the collection/data structure. A deep copy is a copy that copies both the collection and its elements. – JB Nizet Oct 17 '17 at 18:08
  • Question is around Shallow copy of Map and it's Operations are in questions and not the Keys and values stored in the Map. Considering just Map, it's not a Shallow Copy. As i said earlier, MAP2 WILL NOT AFFECT ORIGINAL MAP 1. However, Keys and values stored inside Map in the form of Entry and DataTables, they are shallow copied – Sunil Singhal Oct 18 '17 at 02:54