ArrayList and CopyOnWriteArrayList in java differ based on following parameters:

1. Inclusion in java

Arraylist was added in Java 1.2 version.

CopyOnWriteArrayList was added in Java 1.5 or Java 5.

2. Thread-safety

Arraylist in java is not synchronized thus is not at all thread safe.

CopyOnWriteArrayList is synchronized in nature. Thus it is thread safe and for the same reason only one thread can access the resources in this class at a time.

3. Performance

As arraylist is non-synchronized so it is better performance wise as multiple threads can access the resources of arraylist at the same time simultaneously.

Being synchronized means only one thread can have access to CopyOnWriteArrayList resources thus impacting its performance speed.

4. ConcurrentModificationException

Arraylist throws ConcurrentModificationException.

CopyOnWriteArrayList doesnot throw ConcurrentModificationException.

5. Fail fast or fail safe

 Arraylist’s  iterator and list iterator are fail fast in nature.

CopyOnWriteArrayList iterator are fail safe in nature.

You may also like to read:

  • ArrayList vs LinkedList
  • ArrayList vs Vector
  • Collection vs Collections
  • ArrayList vs HashSet
  • Iterator vs Enumeration

Source link

Difference: ArrayList vs CopyOnWriteArrayList in java

Leave a Reply

Your email address will not be published. Required fields are marked *