I have created new list from old list by using addAll()
method, when i tried to modify new list, the old list is getting modified as well.
For Example -
List<A> list = new ArrayList<A>();
list.add(new A("something1"));
list.add(new A("something2"));
list.add(new A("something3"));
list.add(new A("something4"));
List<A> newList = new ArrayList<A>();
newList.addAll(list);
for(A a1 : newList){
a1.setA("something new");
}
System.out.println("list----------------");
for(A a1 : list){
System.out.println(a1.toString());
}
System.out.println("new list----------------");
for(A a1 : newList){
System.out.println(a1.toString());
}
Output -
list----------------
A [a=something new]
A [a=something new]
A [a=something new]
A [a=something new]
new list----------------
A [a=something new]
A [a=something new]
A [a=something new]
A [a=something new]
How to prevent this behavior and leave the old list as is ?