Wednesday, April 20, 2022

CopyOnWriteArrayList In Java

CopyOnWriteArrayList:

public class CopyOnWriteArrayList<E>
extends Object
implements List<E>, RandomAccess, Cloneable, Serializable

All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess

  • CopyOnWriteArrayList belong to java.util.concurrent package.
  • CopyOnWriteArrayList class is introduced in JDK 1.5
  • CopyOnWriteArrayList Implements the List interface.
  • CopyOnWriteArrayList is a thread-safe variant of ArrayList.
  • null can be added to the list.
  • Iterator of CopyOnWriteArrayList will never throw ConcurrentModificationException. That means fail-safe 
  • CopyOnWriteArrayList creates a Cloned copy of underlying ArrayList, for every update operation and synchronized automatically.
  • Iterator of CopyOnWriteArrayList we can not perform remove, set and add operation otherwise it will throw UnsupportedOperationException at Run-time exception. Iterator of CopyOnWriteArrayList will never throw ConcurrentModificationException.
CopyOnWriteArrayList Example:
package com.javaiq.in;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

public class CopyOnWriteArrayListExample {

	public static void main(String args[]) {
		// create an array list
		CopyOnWriteArrayList cowList = new CopyOnWriteArrayList();
		System.out.println("Initial size of CopyOnWriteArrayList: " + cowList.size());

		// add elements to the CopyOnWriteArrayList
		cowList.add("Amit");
		cowList.add("Sumit");
		cowList.add("Sonu");
		cowList.add("Raj");
		cowList.add("Kapil");
		cowList.add("PK");
		cowList.add(1, "JK");
		System.out.println("Size of CopyOnWriteArrayList after add elements: " + cowList.size());

		// display list elements
		System.out.println("Contents of CopyOnWriteArrayList: " + cowList);

		// Remove elements from the CopyOnWriteArrayList
		cowList.remove("Amit");
		cowList.remove(3);
		System.out.println("Size of al after deletions: " + cowList.size());
		System.out.println("Contents of CopyOnWriteArrayList: " + cowList);

		try {
			Iterator iterator = cowList.iterator();
			while (iterator.hasNext()) {
				iterator.remove();
			}
		} catch (UnsupportedOperationException e) {
			System.out.println("Remove Method does not supported:");
		}

		System.out.println("Size of CopyOnWriteArrayList: " + cowList.size());
	}
}

Output:

Initial size of CopyOnWriteArrayList: 0
Size of CopyOnWriteArrayList after add elements: 7
Contents of CopyOnWriteArrayList: [Amit, JK, Sumit, Sonu, Raj, Kapil, PK]
Size of al after deletions: 5
Contents of CopyOnWriteArrayList: [JK, Sumit, Sonu, Kapil, PK]
Remove Method does not supported:
Size of CopyOnWriteArrayList: 5

How does CopyOnWriteArrayList work internally in Java?

How does CopyOnWriteArrayList work in Java?

Is CopyOnWriteArrayList synchronized?

Yes, CopyOnWriteArrayList is to be used in a Multi Thread(concurrent environment.) based environment. this is useful where read operations are very frequent and update operations are rare.

What is difference between ArrayList and CopyOnWriteArrayList?

Related Tutorials 

No comments:

Post a Comment