Friday, 11 April 2014

poll : Retrieves and removes the head of the queue

E poll()
This method is equivalent to pollFirst(). return the first element of this deque, or null if this deque is empty.

import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

class DequePoll{
 public static void main(String args[]){
  Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (4);
  
  /* Add Elements to Deque */
  myDeque.add(10);
  myDeque.add(20); 
  myDeque.add(30); 
  myDeque.add(40);
  
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.poll());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.poll());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.poll());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.poll());
  System.out.println("Elements in Deque are " + myDeque);
 }
}

Output
Elements in Deque are [10, 20, 30, 40]

Removing the Element from myDeque 10
Elements in Deque are [20, 30, 40]

Removing the Element from myDeque 20
Elements in Deque are [30, 40]

Removing the Element from myDeque 30
Elements in Deque are [40]

Removing the Element from myDeque 40
Elements in Deque are []

1. Return null if the deque is empty
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

class DequePollNull{
 public static void main(String args[]){
  Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (4);
  
  /* Add Elements to Deque */
  myDeque.add(10);
  myDeque.add(20); 
  myDeque.add(30); 
  myDeque.add(40);
  
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.poll());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.poll());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.poll());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.poll());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.poll());
  System.out.println("Elements in Deque are " + myDeque);
 }
}

Output

Elements in Deque are [10, 20, 30, 40]

Removing the Element from myDeque 10
Elements in Deque are [20, 30, 40]

Removing the Element from myDeque 20
Elements in Deque are [30, 40]

Removing the Element from myDeque 30
Elements in Deque are [40]

Removing the Element from myDeque 40
Elements in Deque are []

Removing the Element from myDeque null
Elements in Deque are []




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment