Skip to content

Instantly share code, notes, and snippets.

@Glorp
Created August 2, 2016 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Glorp/f8399fca93afc7e9993d4d7b1c8d3f21 to your computer and use it in GitHub Desktop.
Save Glorp/f8399fca93afc7e9993d4d7b1c8d3f21 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Mutability {
public static void main(String[] args) {
ArrayList<String> l1 = new ArrayList<>();
l1.add("foo");
l1.add("bar");
ArrayList<String> l2 = new ArrayList<>();
l2.add("foo");
l2.add("bar");
Set<List<String>> s = new HashSet<>();
s.add(l1);
System.out.printf("s.size() = %s; s.contains(l1) = %s; s.contains(l2) = %s;\n", s.size(), s.contains(l1), s.contains(l2));
l1.add("quux");
System.out.printf("s.size() = %s; s.contains(l1) = %s; s.contains(l2) = %s;\n", s.size(), s.contains(l1), s.contains(l2));
}
}
@Glorp
Copy link
Author

Glorp commented Aug 2, 2016

javac Mutability.java
java Mutability
s.size() = 1; s.contains(l1) = true; s.contains(l2) = true;
s.size() = 1; s.contains(l1) = false; s.contains(l2) = false;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment