poll( ) method of Queue Interface in Java.

poll( ) method of Queue Interface in Java.


Posted in : Core Java Posted on : January 31, 2011 at 6:08 PM Comments : [ 0 ]

In this section we will discuss how can poll( ) method be implemented in Queue interface in java

poll( ) method of Queue Interface in Java.

In this section we will discuss how can poll( ) method be implemented in Queue interface in java

Syntax

E poll()

This method removes the head element of the queue.

In this method you can retrieve and delete the element at the head of the queue. Precisely an element that is removed from the queue is an operation of the queue's arranging policy which may be different according to its implementation. The remove( ) method is different from this method only by their behavior, poll( ) method returns 'null' if the queue is empty whereas remove( ) method throws 'NoSuchElementException' exception.

Parameter description 

This method does not take any argument but, it returns the head of the queue or 'null' if the underlying queue is empty.

Example of poll( ) method 

In this example we will show you, how does poll( ) method work in Queue interface. This Example will help you to understand  how an element can be removed from the head of the queue. Through this example we will show how can you delete the top element of the underlying Queue, and count the total number of elements in queue before and after removing the elements. We also show the removed element which will be returned by the poll( ) method from queue.

 Example :

package devmanuals.com;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Iterator;
public class QueuePoll {
  public static void main(String[] args) {
    Queue<String> q = new LinkedList<String>();    
    q.add("A");
    q.add("B");
    q.add("C");
    q.add("D");
    q.add("E");
    System.out.println("Is queue empty : "+q.isEmpty());
    System.out.println("Elements of queue : "+q);
    System.out.println("Size of queue before removing element : "+q.size());
    System.out.println("Now removes the head of the queue then queue is ");
    Object obj=q.poll();
    System.out.println(q);
    System.out.println("Removed element = "+obj);
    System.out.println("Size of queue after removing element : "+q.size());
  System.out.println("Removes all the elements from queue");
    Iterator lq = q.iterator();
  while(lq.hasNext())
  {
    q.poll();
  }
  System.out.println(q);
  System.out.println("Is queue empty : "+q.isEmpty());
  //Here implementation of the poll( ) method will display 'null' 
  System.out.println(q.poll());
  }
}

Output :

Is queue empty : false

Elements of queue : [A, B, C, D, E]

Size of queue before removing element : 5

Now removes the head of the queue then queue is

[B, C, D, E]

Removed element = A

Size of queue after removing element : 4

Removes all the elements from queue

[]

Is queue empty : true

null

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics