The iterator() method of AbstractList class in Java


The iterator() method of AbstractList class is used to return an iterator over the elements in this list in proper sequence.

The syntax is as follows

public Iterator<E> iterator()

Here, the Iterator is an iterator over a collection. To work with the AbstractList class, import the following package

import java.util.AbstractList;

The following is an example to implement iterator() method of the AbstractlList class in Java

Example

 Live Demo

import java.util.ArrayList;
import java.util.Iterator;
import java.util.AbstractList;
public class Demo {
   public static void main(String[] args) {
      AbstractList<Integer>
      myList = new ArrayList<Integer>();
      myList.add(75);
      myList.add(100);
      myList.add(150);
      myList.add(200);
      myList.add(250);
      myList.add(150);
      myList.add(150);
      myList.add(400);
      System.out.println("Elements in the AbstractList = " + myList);
      Iterator i = myList.iterator();
      System.out.println("Iterating over the elements in the AbstractList =" );
      while (i.hasNext()) {
         System.out.println(i.next());
      }
   }
}

Output

Elements in the AbstractList = [75, 100, 150, 200, 250, 150, 150, 400]
Iterating over the elements in the AbstractList =
75
100
150
200
250
150
150
400

Updated on: 30-Jul-2019

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements