I have to add the items of one list to another, what is the preferred way and why ?
List<Integer> list1 = new ArrayList();
list1.add(1);
list1.add(2);
list1.add(3);
Method #1:
List<Integer> list2 = list1;
Method #2:
List<Integer> list2 = new ArrayList();
list2.addAll(list1)
AFAIK that in first method list1's reference will be assigned to list2 so any modification to list1 will effect to list2, Am i correct ?
Is there any side effect of using first over second or vice-versa ?
Thanks in advance.
Edit:
I have seen the answer:, I just wanted to know how both of the above approaches are different and what's the trade off using one above other ?