The toString() method of CopyOnWriteArrayList in Java

Java 8ProgrammingObject Oriented Programming

To get the string representation of the CopyOnWriteArrayList, use the toString() method in Java.

The syntax is as follows

String toString()

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 toString() 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(100);
      arrList.add(250);
      arrList.add(0, 400);
      arrList.add(1, 500);
      arrList.add(2, 650);
      arrList.add(700);
      arrList.add(800);
      System.out.println("CopyOnWriteArrayList String Representation = " + arrList.toString());
   }
}

Output

CopyOnWriteArrayList String Representation = [400, 500, 650, 100, 250, 700, 800]
raja
Published on 26-Mar-2019 11:38:53
Advertisements