How to add elements in Java CopyOnWriteArrayList?


To add elements in CopyOnWriteArrayList class in Java, use the add() method. Here, the element is appended to the end of the list.

The syntax is as follows

boolean add(E e)

Here, the parameter e is the element to be appended to this list.

To work with CopyOnWriteArrayList class, you need to import the following package

import java.util.concurrent.CopyOnWriteArrayList;

The following is an example to add elements in the CopyOnWriteArrayList class in Java

Example

 Live Demo

import java.util.concurrent.CopyOnWriteArrayList;
public class Demo {
   public static void main(String[] args) {
      CopyOnWriteArrayList<Integer>
      arrList = new CopyOnWriteArrayList<Integer>();
      arrList.add(100);
      arrList.add(250);
      arrList.add(400);
      arrList.add(500);
      arrList.add(650);
      arrList.add(700);
      arrList.add(800);
      System.out.println("CopyOnWriteArrayList Elements = " + arrList);
   }
}

Output

CopyOnWriteArrayList Elements = [100, 250, 400, 500, 650, 700, 800]

Updated on: 30-Jul-2019

109 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements