The clear() method of CopyOnWriteArrayListin Java

Java 8Object Oriented ProgrammingProgramming

To remove all the elements from the CopyOnWriteArrayList, use the clear() method. It empties the list.

The syntax is as follows:

void clear()

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

import java.util.concurrent.CopyOnWriteArrayList;

The following is an example to implement CopyOnWriteArrayList class clear() method 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(220);
      arrList.add(250);
      arrList.add(400);
      arrList.add(500);
      arrList.add(650);
      arrList.add(700);
      arrList.add(800);
      System.out.println("CopyOnWriteArrayList String Representation = " + arrList.toString());
      arrList.clear();
      System.out.println("Updated CopyOnWriteArrayList = " + arrList.toString());
   }
}

Output

CopyOnWriteArrayList String Representation = [220, 250, 400, 500, 650, 700, 800]
Updated CopyOnWriteArrayList = []
raja
Published on 02-Apr-2019 12:50:19
Advertisements