iterator() method of BlockingDeque Interface in Java.

iterator() method of BlockingDeque Interface in Java.


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

In this method an iterator is returned across the elements in the order from top (first element) to bottom (last element).

iterator() method of BlockingDeque Interface in Java.

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

Syntax

Iterator<E> iterator()

This method gives back an iterator across the elements in proper sequence of an underlying deque.

In this method an iterator is returned across the elements in the order from top (first element) to bottom (last element).

Parameter description

This method has no parameter.

Example of iterator() maethod

In this example we will show you how does an iterator() method work in BlockingDeque interface. This example will help you to understand how can you iterate over the elements into the underlying deque. Through this example we will also show you the displaying of elements from head to tail.

Example :

package devmanuals.com;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.Iterator;
class Traverse implements Runnable 
{
  BlockingDeque<Integer> bdq;
  public Traverse(BlockingDeque<Integer> bdq
  {
    this.bdq = bdq;
  }
  public void run() 
  {
    System.out.println("Elements of deque are : ");
    int i;
    for(i=0; i<5;i++)
      bdq.add(i);
      System.out.println(bdq);
      System.out.println("Size of deque = "+bdq.size());
    try {
      Iterator it= bdq.iterator();
      Thread.sleep(500);
      System.out.println("Elements from head to last");
      while(it.hasNext()==true){
        Thread.sleep(1000);
        System.out.println(it.next());
      }
      }
      catch (InterruptedException e
      {
        e.printStackTrace();
      }
  }
}
public class BdqIterator {
  public static void main(String[] args) {
    BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>();
    Runnable a = new Traverse(bdq);
    new Thread(a).start();
  }
}

Output :

Elements of deque are :

[0, 1, 2, 3, 4]

Size of deque = 5

Elements from head to last

0

1

2

3

4

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics