The first letter simply replaces its link to the list with those provided by the callers. If the old list is not available anywhere, it is entitled to GC.
The second uses the existing list of objects and copies into it all the links to the elements of the caller list. If this increases the size of the collection, then this may include allocating a larger array from the inside (which makes assembling a smaller array).
The reachability of the objects themselves does not change; both versions force lists to refer to the same elements.
The first is usually pretty fast, since you are replacing one link. This works less than copying a bunch of links from one list to another.
Secondly, although it is usually better in terms of encapsulation. Imagine this ...
yourThingie.set(myItems); myItems.add(objectYouNeverWouldHaveAllowed);
In the first version, _data now contains objectYouNeverWouldHaveAllowed . You can no longer apply class restrictions on _data ; since you let the caller bring their own list there, they now have control over the internal state of your property. They can tear your object apart, even by accident.
The second one has no such problem. You retain control of _data , subsequent changes to the caller in their list do not affect you, and if you do not do something broken (for example, provide the recipient who retrieves the naked object), you are safe.
source share