Friday, 11 April 2014

remove : Retrieves and removes the head of the queue

E remove()
This method is equivalent to removeFirst(). Retrieves and removes the head of the queue.
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

class DequeRemove{
 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.remove());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.remove());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.remove());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.remove());
  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. throws NoSuchElementException if this deque is empty
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

class DequeRemoveNoSuch{
 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.remove());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.remove());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.remove());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.remove());
  System.out.println("Elements in Deque are " + myDeque);
  
  System.out.println("\nRemoving the Element from myDeque " + myDeque.remove());
  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 []
Exception in thread "main" java.util.NoSuchElementException
        at java.util.concurrent.LinkedBlockingDeque.removeFirst(LinkedBlockingDeque.java:450)
        at java.util.concurrent.LinkedBlockingDeque.remove(LinkedBlockingDeque.java:670)
        at DequeRemoveNoSuch.main(DequeRemoveNoSuch.java:28


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment