Skip to content

Instantly share code, notes, and snippets.

@kirkbackus
Created July 8, 2015 14:52
Show Gist options
  • Save kirkbackus/e96ad7df18a80190e994 to your computer and use it in GitHub Desktop.
Save kirkbackus/e96ad7df18a80190e994 to your computer and use it in GitHub Desktop.
Testing the shallow copy of an object to see if the second map keeps the original objects around after the first map is cleared. Turns out, the second map does in fact keep the objects around!
import java.util.Map;
import java.util.HashMap;
public class TestShallowCopy {
public static void main(String[] args) {
Map<String, Integer> a = new HashMap<String, Integer>();
a.put("TEST", 1);
Map<String, Integer> b = new HashMap<String, Integer>(a);
a.clear();
System.out.println("TEST: "+b.get("TEST"));
System.out.println("A-size: "+a.size());
System.out.println("B-size: "+b.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment