The listIterator() method of CopyOnWriteArrayList in Java

Java 8Object Oriented ProgrammingProgramming

The listIterator() method of the CopyOnWriteArrayList class in Java is used to return a list iterator over the elements in this list.

The syntax is as follows

public ListIterator<E> listIterator()

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 listIterator() method in Java

Example

 Live Demo

import java.util.Iterator;
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(40);
      arrList.add(60);
      arrList.add(70);
      arrList.add(90);
      arrList.add(100);
      arrList.add(120);
      System.out.println("CopyOnWriteArrayList = " + arrList);
      Iterator i = arrList.listIterator();
      System.out.println("Iterating the values...");
      while (i.hasNext()) {
         System.out.println(i.next());
      }
   }
}

Output

CopyOnWriteArrayList = [30, 40, 60, 70, 90, 100, 120]
Iterating the values...
30
40
60
70
90
100
120
raja
Published on 28-Mar-2019 08:06:59
Advertisements