| Author |
Difference between add() and offer() in Queue
|
Satyajeet Kadam
Ranch Hand
Joined: Oct 19, 2006
Posts: 202
|
|
Q1) What is difference between add() and offer() in priorityQueue
As per both methods throw an ClassCastException if element that is to be added cannot be compared with elements already present in the Queue.
NullPointerException if the specified element is null.
I found the add() internally calls the offer()
Q2)why there are two methods for same operations
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
they are same. but the methods are used in different context.
Queue is a Collection. and offer() is in Queue. consider below code,
|
 |
Satyajeet Kadam
Ranch Hand
Joined: Oct 19, 2006
Posts: 202
|
|
|
Can you please elaborate your answer?
|
 |
Eduardo Mineo
Ranch Hand
Joined: Sep 26, 2011
Posts: 63
|
|
Satyajeet, you can see in SunOracle Queue specification:
The offer method inserts an element if possible, otherwise returning false. This differs from the Collection.add method, which can fail to add an element only by throwing an unchecked exception. The offer method is designed for use when failure is a normal, rather than exceptional occurrence, for example, in fixed-capacity (or "bounded") queues.
--eduardo
|
 |
 |
|
|
subject: Difference between add() and offer() in Queue
|
|
|