Sunday, 6 April 2014

remove : Retrieves and removes the head of this queue

E remove()
Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty.

import java.util.*;
class QueueRemove{
 public static void main(String args[]){
  Queue<Integer> myQueue = new PriorityQueue<> ();
  
  /* Add Elements to myQueue */
  myQueue.add(10);
  myQueue.add(20);
  myQueue.add(30);
  myQueue.add(40);
  myQueue.add(50);
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.remove() + " removed from the Queue ");
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.remove() + " removed from the Queue ");
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.remove() + " removed from the Queue ");
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.remove() + " removed from the Queue ");
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.remove() + " removed from the Queue ");
  
 }
}

Output
Elements in myQueue are
[10, 20, 30, 40, 50]
Head Element 10 removed from the Queue

Elements in myQueue are
[20, 40, 30, 50]
Head Element 20 removed from the Queue

Elements in myQueue are
[30, 40, 50]
Head Element 30 removed from the Queue

Elements in myQueue are
[40, 50]
Head Element 40 removed from the Queue

Elements in myQueue are
[50]
Head Element 50 removed from the Queue

1. throws NoSuchElementException if this queue is empty
import java.util.*;
class QueueRemoveNoSuch{
 public static void main(String args[]){
  Queue<Integer> myQueue = new PriorityQueue<> ();
  
  /* Add Elements to myQueue */
  myQueue.add(10);
  myQueue.add(20);
  myQueue.add(30);
  myQueue.add(40);
  myQueue.add(50);
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.remove() + " removed from the Queue ");
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.remove() + " removed from the Queue ");
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.remove() + " removed from the Queue ");
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.remove() + " removed from the Queue ");
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.remove() + " removed from the Queue ");
  
  System.out.println("\nElements in myQueue are");
  System.out.println(myQueue);
  System.out.println("Head Element " + myQueue.remove() + " removed from the Queue ");
  
 }
}

Output

Elements in myQueue are
[10, 20, 30, 40, 50]
Head Element 10 removed from the Queue

Elements in myQueue are
[20, 40, 30, 50]
Head Element 20 removed from the Queue

Elements in myQueue are
[30, 40, 50]
Head Element 30 removed from the Queue

Elements in myQueue are
[40, 50]
Head Element 40 removed from the Queue

Elements in myQueue are
[50]
Head Element 50 removed from the Queue

Elements in myQueue are
[]
Exception in thread "main" java.util.NoSuchElementException
        at java.util.AbstractQueue.remove(Unknown Source)
        at QueueRemoveNoSuch.main(QueueRemoveNoSuch.java:35)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment