Difference between add() and offer() method in java
Sl. No. | add() | offer() |
---|---|---|
1 | add() will throw an IllegalStateException if no space is currently available in the Queue, otherwise add method will return true. | offer() method will return false if the element cannot be inserted due to capacity restrictions. |
2 | add() method comes from the Collections framework. | offer() method comes from the queue. |
3 | add() method always returns true. | offer() method always returns false. |
4 | add() method throws an exception when no addition is possible in queue. | offer() method throws true or false if addition is done in the queue. |
Difference between add() and offer() methods in Queue in Java
Add() and Offer() are the methods used for adding the elements in the Queue. But both have their main function and they treat the elements differently.