Home  >  Core Java

Example of CopyOnWriteArrayList in Java

By Arvind Rai, December 21, 2012
1. CopyOnWriteArrayList is thread safe.
2. The operations like add, set in CopyOnWriteArrayList are made by taking fresh copy.
3. While iterating CopyOnWriteArrayList, we can not add are remove or modify.
4. CopyOnWriteArrayList is used in collection framework and this is available from jdk 1.5.
package com.concretepage;

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

public class CopyOnWriteArrayListTest {
	public static void main(String... args){
		CopyOnWriteArrayList ob= new CopyOnWriteArrayList();
		
		ob.add("A");
		ob.add("B");
		ob.add("C");
		ob.add("D");
		ob.add("E");
		
		ob.addIfAbsent("A");
		
		ob.remove("B");
		Iterator itr= ob.listIterator();
		while(itr.hasNext()){
			System.out.println(itr.next());
		}
	}

}
Output
A
C
D
E
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us