iterator() method of Deque Interface in Java.

iterator() method of Deque Interface in Java.


Posted in : Core Java Posted on : February 5, 2011 at 4:32 PM Comments : [ 0 ]

In this method the elements of underlying deque are returned by an iterator in their sequence. This method will returned the elements of deque in chronological order from top to bottom.

iterator() method of Deque Interface in Java.

In this section we will discuss how can iterator() method be implemented in Deque interface in java.

Syntax

Iterator<E> iterator()

This method returns the elements of deque in proper sequence.

In this method the elements of underlying deque are returned by an iterator in their sequence. This method will returned the elements of deque in chronological order from top to bottom.

Parameter description

This method has no parameter.

Example of iterator() method

In this example we will show how does iterator() method work in Deque interface. This example will help you to understand how you can find the elements of deque in their order. In this example we will make a LinkedList type Deque and insert the element into this deque using add(e) method finally, we will display elements of this deque in their sequence using iterator() method with some more implementation of Iterator interface methods such as hasNext() and next().

Example :

package devmanuals.com;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Iterator;
public class DequeIterator {
  public static void main(String args[]) {
    Deque<Integer> dq = new LinkedList<Integer>();
    dq.add(20);
    dq.add(21);
    dq.add(22);
    dq.add(23);
    dq.add(24);
    dq.add(25);
    System.out.println("Elements of deque : " + dq);
    System.out.println("Elements of deque in their sequence:");
    Iterator it =dq.iterator();
     while (it.hasNext())
      System.out.println(it.next());
     System.out.println("Size of deque is : "+dq.size());
  }
}

Output :

Elements of deque : [20, 21, 22, 23, 24, 25]

Elements of deque in their sequence:

20

21

22

23

24

25

Size of deque is : 6 

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics