Sunday, 6 April 2014

offer : Insert Element

boolean offer(E e)
Inserts the specified element into this queue. return true if the element was added to this queue, else false.

import java.util.*;
class QueueOffer{
 public static void main(String args[]){
  Queue<Integer> myQueue = new PriorityQueue<> ();
  
  /* Add Elements to myQueue */
  System.out.println("Is 10 Added to the Queue " + myQueue.offer(10));
  System.out.println("Is 20 Added to the Queue " + myQueue.offer(20));
  System.out.println("Is 30 Added to the Queue " + myQueue.offer(30));
  System.out.println("Is 40 Added to the Queue " + myQueue.offer(40));
  System.out.println("Is 50 Added to the Queue " + myQueue.offer(50));
  System.out.println("Is 60 Added to the Queue " + myQueue.offer(60));
  
  System.out.println("Elements in myQueue are");
  System.out.println(myQueue);

 }
}

Output
Is 10 Added to the Queue true
Is 20 Added to the Queue true
Is 30 Added to the Queue true
Is 40 Added to the Queue true
Is 50 Added to the Queue true
Is 60 Added to the Queue true
Elements in myQueue are
[10, 20, 30, 40, 50, 60]

 1. throws ClassCastException if the class of the specified element prevents it from being added to this queue
import java.util.*;
class QueueOfferClassCast{
 public static void main(String args[]){
  Queue<QueueOfferClassCast> myQueue = new PriorityQueue<> ();
  
  QueueOfferClassCast obj1 = new QueueOfferClassCast();
  QueueOfferClassCast obj2 = new QueueOfferClassCast();
  
  /* Add Elements to myQueue */
  System.out.println("Is obj1 Added to the Queue " + myQueue.offer(obj1));
  System.out.println("Is obj2 Added to the Queue " + myQueue.offer(obj1));
  
  System.out.println("Elements in myQueue are");
  System.out.println(myQueue);
 }
}

myQueue is of type QueueOfferClassCast, But QueueOfferClassCast not implements Comparable interface, so While trying to run the above program, ClassCastException thrown.

Is obj1 Added to the Queue true
Exception in thread "main" java.lang.ClassCastException: QueueOfferClassCast can not be cast to java.lang.Comparable
        at java.util.PriorityQueue.siftUpComparable(Unknown Source)
        at java.util.PriorityQueue.siftUp(Unknown Source)
        at java.util.PriorityQueue.offer(Unknown Source)
        at QueueOfferClassCast.main(QueueOfferClassCast.java:11)

2.throws NullPointerException if the specified element is null and this queue does not permit null elements
import java.util.*;
class QueueOfferNull{
 public static void main(String args[]){
  Queue<Integer> myQueue = new PriorityQueue<> ();
  
  /* Add Elements to myQueue */
  System.out.println("About to add null to Priority Queue");
  myQueue.offer(null);
  
  System.out.println("Elements in myQueue are");
  System.out.println(myQueue);
 }
}

Output
About to add null to Priority Queue
Exception in thread "main" java.lang.NullPointerException
        at java.util.PriorityQueue.offer(Unknown Source)
        at QueueOfferNull.main(QueueOfferNull.java:8)

3. throws IllegalArgumentException if some property of this element prevents it from being added to this queue


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment