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.

Similar Reads

add() method in Java:

It Inserts the specified element to the end of the queue if there is space, returning true upon success and throwing an IllegalStateException if no space is currently available. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false....

Offer() method in Java:

...

Difference between add() and offer() method in java

...