Reputation: 17140
If I create a new HashMap and a new List, and then place the List inside the Hashmap with some arbitrary key and then later call List.clear()
will it affect what I've placed inside the HashMap?
The deeper question here being: When I add something to a HashMap, is a new object copied and placed or is a reference to the original object placed?
Thanks!
Upvotes: 40
Views: 62845
Reputation: 10684
Map<Integer, Integer> hasmapA = new HashMap<>();
hasmapA.put("key1", "value1");
hasmapA.put("key2", "value2");
hasmapA.put("key3", "value3");
Copy By reference: If you assign one HashMap to other then both point to same reference in memory.
Map hasmapB;
hashmapB = hashmapA;
If you make changes in any of these, changes will reflect in both HashMap as both are referencing to same location.
clone/deepcopy/create separate memory location/create separate object
of hashmapB while copying content of hashmapA
Map<Integer, Integer> hashmapB = new HashMap<>();;
hashmapB.putAll(hashmapA)
Note: ** Have you noticed difference in both points for hashmapB declaration? In second point we have to call **HashMap constructor. So that we can putAll data of hashmapA into hashmapB.
Upvotes: 5
Reputation: 11
The Correct answer for this is Explained below : Suppose you have a HashMap called hashmap and initially you put a key value pair in this HashMap e.g. hashmap<"test1","test2">. After this when you pass this hashmap to a function where you again changes its value to test3 like hashmap.put("test1", "test3") and print the map again in main Method, the Java Pass by Value Concept fails here.
The Reason is : When you use HashMap, it does the Hashing for the Key(test1) and store the value. When you passed it to the function where it again changes its value, it again does the hashing for the same key and gets the same memory Address and changes the value Accordingly. Thats why when you try to retreive the key "test1" it gives you the result as "test3"
Upvotes: 1
Reputation: 19
Try it out
package test32; import java.util.ArrayList; import java.util.HashMap; import java.util.List; class Foo { public Foo(int id, String name) { this.id=id; this.name=name; } public static void main(String[] args) { HashMap strs = new HashMap(); // create a list of objects List ls = new ArrayList(); ls.add(new Foo(1, "Stavros")); ls.add(new Foo(2, "Makis")); ls.add(new Foo(3, "Teo")); ls.add(new Foo(4, "Jim")); // copy references of objects from list to hashmap strs.put("1", ls.get(0)); strs.put("2", ls.get(1)); strs.put("3", ls.get(2)); strs.put("4", ls.get(3)); System.out.println("list before change : " + ls); System.out.println("map before change: " + strs); // get an object from the hashmap Foo f=strs.get("1"); // set a different value f.setId(5); // observe that the differences are reflected to the list and to the hashmap also System.out.println("list after change : "+ls); System.out.println("map after change: "+strs); } private int id; public void setId(int id) { this.id=id; } public int getId() { return this.id; } private String name; public void setName(String name) { this.name=name; } public String getName() { return this.name; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append(id); sb.append("-"); sb.append(name); return sb.toString(); } }
Upvotes: 1
Reputation: 75376
Generally you always deal with references in Java (unless you explicitly create a new object yourself with "new" [1]).
Hence it is a reference and not a full object copy you have stored in the map, and changing the list will also effect what you see when going through the map.
It's a feature, not a bug :)
[1] Puritans will include "clone()" and serialization, but for most java code "new" is the way to get objects.
Upvotes: 1
Reputation: 583
When I add something to a HashMap, is a new object copied and placed or is a reference to the original object placed?
It is always a reference to the object. If you clear the HashMap the object will be still "live". Then the object will be destroyed by the garbage collector if no one is referencing it anymore. If you need to copy it, take a look to Object.clone() method and to the Cloneable interface
Upvotes: 5
Reputation: 3610
Geeze people...everything in Java is pass by value. When you pass an object, the value you are passing is the object reference. Specifically, you are passing a copy of the object reference. There are no pointers in Java either, though references are similar. Get it right!
Upvotes: -2
Reputation: 30642
What's happening here is that you're placing a pointer to a list in the hashmap, not the list itself.
When you define
List<SomeType> list;
you're defining a pointer to a list, not a list itself.
When you do
map.put(somekey, list);
you're just storing a copy of the pointer, not the list.
If, somewhere else, you follow that pointer and modify the object at its end, anyone holding that pointer will still be referencing the same, modified object.
Please see http://javadude.com/articles/passbyvalue.htm for details on pass-by-value in Java.
Upvotes: 50
Reputation: 1636
Java is pass-by-reference-by-value.
Adding the list to the hash map simply adds the reference to hash map, which points to the same list. Therefore, clearing the list directly will indeed clear the list you're referencing in the hashmap.
Upvotes: 13