dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Jun 26, 2018

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Understanding CopyOnWriteArrayList

In CopyOnWriteArrayList, operations such as add, remove, etc., are implemented on a copy of the of the array. It definitely impacts performance but the trade-off is that the data remains intact.

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

public class UsingCopyOnWriteArrayList
{
    public static void main(String args[])
   {
        CopyOnWriteArrayList copyOnWriteArrayList = new CopyOnWriteArrayList();
        copyOnWriteArrayList.add("United Status");
        copyOnWriteArrayList.add("France");
        copyOnWriteArrayList.add("China");
      copyOnWriteArrayList.add("Russia");
      copyOnWriteArrayList.add("United Kingdom");
      
        Iterator iteratorForCopyOnWriteArrayList = copyOnWriteArrayList.iterator();
      System.out.println("Iterating CopyOnWriteArrayList");
        while(iteratorForCopyOnWriteArrayList.hasNext()){
         System.out.println("Element : " + iteratorForCopyOnWriteArrayList.next());
        }
    }
}

/*

Expected output:

[root@mypc]# java UsingCopyOnWriteArrayList
Iterating CopyOnWriteArrayList
Element : United Status
Element : France
Element : China
Element : Russia
Element : United Kingdom

*/
M S Sridhar
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date