The clone() method of CopyOnWriteArrayList method in Java

Java 8Object Oriented ProgrammingProgramming

To return a shallow copy of the CopyOnWriteArrayList, use the clone() method.

The syntax is as follows:

public Object clone()

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 clone() 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(30);
      arrList.add(90);
      arrList.add(80);
      arrList.add(70);
      arrList.add(90);
      arrList.add(100);
      arrList.add(120);
      System.out.println("CopyOnWriteArrayList = " + arrList);
      System.out.println("Cloned CopyOnWriteArrayList = " + arrList.clone());
   }
}

Output

CopyOnWriteArrayList = [30, 90, 80, 70, 90, 100, 120]
Cloned CopyOnWriteArrayList = [30, 90, 80, 70, 90, 100, 120]
raja
Published on 01-Apr-2019 13:27:18
Advertisements