0

I am not able to understand why can't we achieve by just iterating over an immutable list rather than using this new implementation?

AKS
  • 1,393
  • 3
  • 19
  • 29
  • Are you asking about the difference between iterating over a `CopyOnWriteArrayList` and iterating over a `List` returned by invoked `Collections.unmodifiableList(..)`? – Sotirios Delimanolis Nov 29 '14 at 17:33
  • The main difference is that it's mutable and thread safe. The immutable list is...immutable. Java does not have any built in immutable types - without telling us what implementation you are using, that is all we can say. – Boris the Spider Nov 29 '14 at 17:34
  • @SotiriosDelimanolis No, My question is related to the relevance of this CopyOnWriteArrayList. And can't we get the same feature by just using an ImmutableList, which is USP(unique selling point) of CopyOnWriteArrayList. – AKS Nov 29 '14 at 17:35
  • 1
    @SotiriosDelimanolis USP is Unique Selling Point. – Boris the Spider Nov 29 '14 at 17:39
  • 1
    You are mixing a lot of concepts here; The characteristic of a COW list is that it is thread safe; the characteristic of a Collections.unmodifiableList() is that it disables modifications for that list. – fge Nov 29 '14 at 17:42

1 Answers1

2

The javadoc of CopyOnWriteArrayList states

A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.

While the javadoc of Collections#unmodifiableList(List) states

Returns an unmodifiable view of the specified list. This method allows modules to provide users with "read-only" access to internal lists.

Query operations on the returned list "read through" to the specified list, and attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException.

So, no, you can't get the features provided by CopyOnWriteArrayList with a List returned by Collections#unmodifiableList(List).

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724