In this shot, we will discuss the Deque.iterator()
method in Java.
The Deque.iterator()
method is present in the Deque
interface inside the java.util
package.
Deque.iterator()
returns the iterator over the deque elements in the sequence they are present in.
Iterator iterator()
The Deque.iterator()
method doesn’t take any parameters.
Iterator
: The iterator over the deque elements in the sequence they are present in.Let’s take a look at the following code snippet.
import java.util.*;class Main{public static void main(String[] args){Deque<Integer> d = new LinkedList<Integer>();d.add(2);d.add(28);d.add(6);d.add(80);d.add(21);d.add(7);d.add(15);Iterator i = d.iterator();System.out.print("The iterator values of deque are: ");while (i.hasNext()){System.out.print(i.next()+" ");}System.out.println();}}
Main
class.main
function.Integer
type elements.Deque.add()
method to insert the elements in the deque.while
loop to check the presence of the next element in the deque with the Iterator.hasNext()
method. We then display the value of the iterator over each element.