Java Utility Library

Java ArrayDeque - iterator() Method



The java.util.ArrayDeque.iterator() method returns an iterator over the elements in the deque. The elements will be ordered from first (head) to last (tail). This is the same order that elements would be dequeued (via successive calls to remove() or popped (via successive calls to pop()).

Syntax

public Iterator<E> iterator()

Here, E is the type of element maintained by the container.


Parameters

No parameter is required.

Return Value

Returns an iterator over the elements in this deque.

Exception

NA.

Example:

In the example below, the java.util.ArrayDeque.iterator() method returns an iterator over the elements of the given deque.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an ArrayDeque
    ArrayDeque<Integer> MyDeque = new ArrayDeque<Integer>();

    //populating the ArrayDeque
    MyDeque.add(10);
    MyDeque.add(20);
    MyDeque.add(30);
    MyDeque.add(40);

    //print the content of the ArrayDeque
    System.out.println("MyDeque contains: " + MyDeque);

    //creating an iterator
    Iterator<Integer> itr = MyDeque.iterator();

    //print the content of the iterator
    System.out.println("The iterator values are: ");
    while(itr.hasNext())
      System.out.println(itr.next()); 
  }
}

The output of the above code will be:

MyDeque contains: [10, 20, 30, 40]
The iterator values are: 
10
20
30
40

❮ Java.util - ArrayDeque