-2

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 ?

pandey_shubham
  • 423
  • 5
  • 14
  • 1
    Does this answer your question? [How to copy Java Collections list](https://stackoverflow.com/questions/689370/how-to-copy-java-collections-list) – Nikhil Singh May 31 '21 at 12:39
  • Yes, as for first method, both `list1` and `list2` will refer to the same data, so any modification will affect another. BTW, the better way for second method is: List list2 = new ArrayList<>(list1); – chenzhongpu May 31 '21 at 13:04

3 Answers3

0
  1. list2 will become just a reference to list1. Any changes on any of them will be applied to both (in fact both is just a reference to same list).
  2. After this you will have access to both list1 and list2 and both will have different references, what means that changes on one, won't affect another.
ulou
  • 5,542
  • 5
  • 37
  • 47
0

If you write

List<Integer> list2 = list1;

... you are making list2 point to list1. If list1 is changed, list2 will change as well. If it's disposed, also list2 will be disposed.

For example:

list2 = list1;
list1.remove(3); //<-- list2 will also reflect remove(3)
list1.add(4); //<-- list2 will also get a 4 at the end of the list

If you use

list2.addAll(list1);
//or list2 = new ArrayList<>(list1);

... then you are creating a new list, independent from the first one, that contains a copy of the elements (in the same order) than list1 at the moment in which you are initializing it. The two lists will be completely independent in their structure.

list2.addAll(list1);
list1.remove(3); //<-- list 2 is unchanged
list1.add(4); //<-- list 2 is unchanged

Note that if list2 contains objects and one of the object changes, it will change both in list1 and list2 (because the list contains the references to the objects, not the values).

This said, the right way depends on what you want to do. If you want to reference list1 into another object and want it to be the mirror of list1, then go for method 1. But most probably, you just want a copy of your list1 at a given instant, so in that case, you should use list2.

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
0

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 ?

Yes, you are correct. List<Integer> list2 = list1; means that you have multiple variables pointing to the same List. If you change the content of one, this also changes the content of the other list.

Is there any side effect of using first over second or vice-versa ?

When using the second methods, you will have multiple independent lists containing the same elements. If you then add/remove items (or similar), the change won't be reflected to the other List.

In fact, you can even pass a Collection to the constructor of ArrayList doing the same thing:

List<Integer> list2=new ArrayList<>(list2);
dan1st
  • 12,568
  • 8
  • 34
  • 67