vsnyc vsnyc - 2 months ago 17
Java Question

Initial capacity for CopyOnWriteArrayList

Many List implementations have an option to specify an initial capacity for the collection, why is this not allowed for CopyOnWriteArrayList?

Answer

In a conventional ArrayList the capacity is a hint to reserve more space in the backing array for more elements to be added to the list later on.

In a CopyOnWriteArrayList, every (atomic) write operation creates a new backing array. There no point in preallocating an array that is bigger than the current list size because that space would never be used.