By looking at the code of Collections class, i got to know that when we are using the method unmodifiableList(List list)
or unmodifiableCollection(Coll
I Found one way to do this is
List unmodifiableList = Collections.unmodifiableList( new ArrayList(modifiableList));
List strings = new ArrayList();
// unmodifiable.add("New string");
strings.add("Aha 1");
strings.add("Aha 2");
List unmodifiable = Collections.unmodifiableList(strings);
List immutableList = Collections.unmodifiableList(new ArrayList<>(strings));
// Need some way to fix it so that Strings does not Modify
strings.add("Aha 3");
strings.add("Aha 4");
strings.remove(0);
for (String str : unmodifiable) {
System.out.println("Reference Modified :::" + str);
}
for (String str : immutableList) {
System.out.println("Reference Modified :::" + str);
}