Santoshkumar Kalasa Santoshkumar Kalasa - 9 days ago 7
Java Question

list.clone is prefered over new ArrayList(originalList)

which is standard(Prefered) way of creating clone

list.clone()


or

new ArrayList(originalList) ??

Answer

Use new ArrayList(originalList) instead of list.clone() because for list you have to iterate for all object of list and clone individually.

This will work fine for Strings, but it is worth noting that ArrayList.clone will perform a shallow copy, so if there were mutable objects in the list, they will not be cloned and changing one in one list will change that one in the other list as well.