Deque iterator() method in Java Last Updated : 20 Jun, 2020 Comments Improve Suggest changes Like Article Like Report 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. Java // 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()); } } } Output: The iterator values of Deque are: 7855642 35658786 5278367 74381793 Program 2: With the help of LinkedList. Java // 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()); } } } Output: The iterator values of Deque are: 7855642 35658786 5278367 74381793 Program 3: With the help of LinkedBlockingDeque. Java // 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()); } } } Output: The iterator values of Deque are: 7855642 35658786 5278367 74381793 Program 4: With the help of ConcurrentLinkedDeque. Java // 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()); } } } 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-- Comment More infoAdvertise with us Next Article Deque iterator() method in Java gopaldave Follow Improve Article Tags : Misc Java Java - util package Java-Functions java-deque +1 More Practice Tags : JavaMisc Similar Reads Deque Interface in Java Deque Interface present in java.util package is a subtype of the queue interface. The Deque is related to the double-ended queue that supports adding or removing elements from either end of the data structure. It can either be used as a queue(first-in-first-out/FIFO) or as a stack(last-in-first-out/ 9 min read Deque add() method in Java The add(E e) method of Deque Interface inserts the element passed in the parameter to the end of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax:  bool 4 min read Deque addFirst() method in Java with Examples The addFirst(E e) method of Deque Interface inserts the element passed in the parameter to the front of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax: 4 min read Deque addLast() method in Java The addLast(E e) method of Deque Interface inserts the element passed in the parameter to the end of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax: bo 4 min read Deque contains() method in Java The contains(E e) method of Deque Interface check for the presence of the element e in the Deque container. If the Deque contains one occurrence of the element, then it returns true else it returns false. Syntax: boolean contains(Object o) Parameters: This method accepts a mandatory parameter o whic 5 min read Deque descendingIterator() Method in Java The descendingIterator() method of the Deque Interface returns an iterator over the elements in this deque in reverse order. The elements will be returned from the last to the first order. This can be used with any class implementing the Deque Interface like LinkedList, ArrayDeque, LinkedBlockingDeq 2 min read Deque element() method in Java The element() method of Deque Interface returns the element at the front the container. It does not deletes the element in the container. This method returns the head of the Deque. The method throws an exception when the Deque is empty. Syntax:  E element() Parameters: This method does not accepts 3 min read Deque getFirst() method in Java The getFirst() method of Deque Interface returns the first element or the head of the Deque. It does not deletes the element. It throws an exception when the Deque is empty. Syntax:  E getFirst() Parameters: This method does not accepts any parameter.Returns: This method returns the first element o 3 min read Deque getLast() method in Java The getLast() method of Deque Interface returns the last element or the tail of the Deque. It does not deletes the element. It throws an exception when the Deque is empty. Syntax: E getLast() Parameters: This method does not accepts any parameter. Returns: This method returns the last element or the 3 min read 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 doe 3 min read Like