title: Concurrent ArrayList
body: <p>I need an ArrayList-like structure allowing just the following operations</p>
  
  <ul>
  <li><code>get(int index)</code></li>
  <li><code>add(E element)</code></li>
  <li><code>set(int index, E element)</code></li>
  <li><code>iterator()</code></li>
  </ul>
  
  <p>Because of the iterator being used in many places, using <code>Collections#synchronizedList</code> would be too error-prone. The list can grow to a few thousand elements and gets used a lot, so I'm pretty sure, that <code>CopyOnWriteArrayList</code> will be too slow. I'll start with it to avoid premature optimizations, but I'd bet it won't work well.</p>
  
  <p>Most accesses will be single-threaded reads. So I'm asking what's the proper data structure for this.</p>
  
  <hr>
  
  <p>I though that wrapping the <code>synchronizedList</code> in something providing a synchronized iterator would do, but it won't because of the <code>ConcurrentModificationException</code>. Concenrning concurrent behavior, I obviously need that all changes will be visible by subsequent reads and iterators. </p>
  
  <p>The iterator doesn't have to show a consistent snapshot, it may or may not see the updates via <code>set(int index, E element)</code> as this operation gets used only to replace an item with its updated version (containing some added information, which is irrelevant for the user of the iterator). The items are fully immutable.</p>
  
  <hr>
  
  <p>I clearly stated why <code>CopyOnWriteArrayList</code> would not do. <code>ConcurrentLinkedQueue</code> is out of question as it lacks an indexed access. I need just a couple of operations rather than a fully fledged <code>ArrayList</code>. So unless <em>any java concurrent list-related</em> question is a duplicate of <a href="https://stackoverflow.com/q/6916385/581205">this question</a>, this one is not.</p>