• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Difference between ArrayList and CopyOnWriteArrayList

May 27, 2015 By j2eereference Leave a Comment

Difference between ArrayList and CopyOnWriteArrayList

We know that ArrayList is not thread safe . Hence any simultaneous thread can access and modify the content of list simultaneously. Here lies the problem of ConcurrentModificationException. When one thread is Iterating over an ArrayList and any other thread or the same thread modify the content of list and when we again call next() method of the iterator ,we get ConcurrentModificationException exception.

Lets find the difference between Difference between ArrayList and CopyOnWriteArrayList with the below example .

ArrayList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.ArrayList;
import java.util.Iterator;
 
public class ArrayListTest {
 
public static void main(String[] args) {
ArrayList al=new ArrayList();
al.add("One");
al.add("Two");
al.add("Three");
Iterator it = al.iterator();
while(it.hasNext()){
String str = it.next();
System.out.println(str);
al.remove("One");
}
}
}

output

1
2
3
4
5
One <span style="color: red;">
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at ArrayListTest.main(ArrayListTest.java:14)</span>

Here you can notice that we got the ConcurrentModificationException  when we called it.next() method. Now lets see why are we getting this exception.

In ArrayList implementation , we have a variable called modCount which holds the modification count and whenever we modify the ArrayList , value of modCount increments. expectedModCount is the iterator variable that is initialized when we create iterator with same value as modCount. Hence iterator throws ConcurrentModificationException if list size is changed.

CopyOnWriteArrayList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
 
public class ArrayListTest {
 
public static void main(String[] args) {
CopyOnWriteArrayList al=new CopyOnWriteArrayList();
al.add("One");
al.add("Two");
al.add("Three");
Iterator it = al.iterator();
while(it.hasNext()){
String str = it.next();
System.out.println(str);
al.remove("One");
}
}
}

Output

One
Two
Three

Here you can notice that we haven’t got ConcurrentModificationException when we used CopyOnWriteArrayList .

Why CopyOnWriteArrayList is required?

Sometimes we may have to add or remove elements from the list while doing the list iteration, in such case we should use Concurrent Collection class – CopyOnWriteArrayList.

Related Posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Difference between Stack and Heap memory
  • What is ThreadMXBean in java?
  • What is serialVersionUID
  • What is exchanger in concurrency?

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.