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

Why two functions to do the same thing?

The description provided in the java api docs at http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html is the same.

+8
java
source share
1 answer

Two functions are performed from two different interfaces that PriorityQueue implements:

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

For a queue with capacity limitation, the difference is that add() always returns true and throws an exception if it cannot add an element, whereas offer() allowed to return false if it cannot add an element.

However, this does not apply to PriorityQueue ; two functions are synonymous.

+21
source share

All Articles