Difference between offer() and add() in priority queue in java? [duplicate]

Solution 1:

The two functions come from two different interfaces that PriorityQueue implements:

  • add() comes from Collection.
  • offer() comes from Queue.

For a capacity-constrained queue, the difference is that add() always returns true and throws an exception if it can't add the element, whereas offer() is allowed to return false if it can't add the element.

However, this doesn't apply to PriorityQueue; the two functions are synonymous.