Deque iterator() method in Java

The iterator() method of Deque Interface returns an iterator over the elements in this deque in a proper sequence. The elements will be returned in order from first (head) to last (tail). The returned iterator is a “weakly consistent” iterator.

Syntax:

Iterator iterator()

Parameters: This method does not accepts any parameter.

Returns: This method returns an iterator over the elements in this deque in a proper sequence.

Below programs illustrate iterator() method of Deque:



Program 1: With the help of ArrayDeque.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate iterator()
// method of Deque
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque<Integer> DQ
            = new ArrayDeque<Integer>();
  
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
  
        // Call iterator() method of Deque
        Iterator iteratorVals = DQ.iterator();
  
        // Print elements of iterator
        // created from Deque
        System.out.println("The iterator values"
                           + " of Deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}

chevron_right


Output:

The iterator values of Deque are:
7855642
35658786
5278367
74381793

Program 2: With the help of LinkedList.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate iterator()
// method of Deque
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque<Integer> DQ
            = new LinkedList<Integer>();
  
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
  
        // Call iterator() method of Deque
        Iterator iteratorVals = DQ.iterator();
  
        // Print elements of iterator
        // created from Deque
        System.out.println("The iterator values"
                           + " of Deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}

chevron_right


Output:

The iterator values of Deque are:
7855642
35658786
5278367
74381793

Program 3: With the help of LinkedBlockingDeque.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate iterator()
// method of Deque
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque<Integer> DQ
            = new LinkedBlockingDeque<Integer>();
  
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
  
        // Call iterator() method of Deque
        Iterator iteratorVals = DQ.iterator();
  
        // Print elements of iterator
        // created from Deque
        System.out.println("The iterator values"
                           + " of Deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}

chevron_right


Output:

The iterator values of Deque are:
7855642
35658786
5278367
74381793

Program 4: With the help of ConcurrentLinkedDeque.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate iterator()
// method of Deque
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque<Integer> DQ
            = new ConcurrentLinkedDeque<Integer>();
  
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
  
        // Call iterator() method of Deque
        Iterator iteratorVals = DQ.iterator();
  
        // Print elements of iterator
        // created from Deque
        System.out.println("The iterator values"
                           + " of Deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}

chevron_right


Output:

The iterator values of Deque are:
7855642
35658786
5278367
74381793

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#iterator–

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : KaashyapMSK