I am struggling with creating ImmutableList Unmodified will only restrict to the produced collection although the Collection from where it had been created can be modified.
public static void main(String[] args) {
List<String> strings = new ArrayList<String>();
// unmodifiable.add("New string");
strings.add("Aha 1");
strings.add("Aha 2");
List<String> unmodifiable = Collections.unmodifiableList(strings);
// Need some way to fix it so that Strings does not Modify
strings.add("Aha 3");
strings.add("Aha 4");
for (String str : unmodifiable) {
System.out.println("Reference Modified :::" + str);
}
List<List<String>> nCopies = Collections.nCopies(3, strings);
for (List<String> innerString : nCopies) {
innerString.add("Aha Inner");
}
for (List<String> innerString : nCopies) {
for (String str : innerString) {
System.out.println(str);
}
}
}