When list.addAll(list2)
is called will objects in lists be copied to list? or just copy their references... did not find any explanation on javadoc...
Now we know that copy constructor or various collection classes like addAll() method of List or Set, only creates a shallow copy of Collection and both original and cloned Collection points to the same objects. To avoid this, we can deep copy collection, iterating over them and cloning each element.
addAll(set); Here, we have used the addAll() method to add all the elements of the hashset to the arraylist. The optional index parameter is not present in the method. Hence, all elements are added at the end of the arraylist.
Return value The addAll() method returns true if the collection changes as a result of elements being added into it; otherwise, it will return false .
add is used when you need to add single element into collection. addAll is used when you want to add all elements from source collection to your collection.
No copy of the objects or their data are made; their references are simply added to the list object.
No, the objects will not be copied; references to the same objects will be added to the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With