Because these methods have different semantics explained in the JavaDoc. add/remove are unconditional while offer/poll return special value:
offeronly offers a new value, but it might not be accepted, e.g. if the queue is fullpollonly polls for the value, but we accept the fact the value might not be there.
To complicate matters more, BlockingQueue introduces yet another pair of methods for blocking add/remove. Of course they could have used the same named with a bunch of parameters/flags,
smellyGet(boolean blocking, boolean failOnEmpty)
but don't you think this is a better design?
| Throws ex. | Special v. | Blocks | Times out
--------+------------+------------+--------+---------------------
Insert | add(e) | offer(e) | put(e) | offer(e, time, unit)
Remove | remove() | poll() | take() | poll(time, unit)
Examine | element() | peek() | N/A | N/A
* https://meta.stackexchange.com/questions/73566
Answer from Tomasz Nurkiewicz on Stack OverflowBecause these methods have different semantics explained in the JavaDoc. add/remove are unconditional while offer/poll return special value:
offeronly offers a new value, but it might not be accepted, e.g. if the queue is fullpollonly polls for the value, but we accept the fact the value might not be there.
To complicate matters more, BlockingQueue introduces yet another pair of methods for blocking add/remove. Of course they could have used the same named with a bunch of parameters/flags,
smellyGet(boolean blocking, boolean failOnEmpty)
but don't you think this is a better design?
| Throws ex. | Special v. | Blocks | Times out
--------+------------+------------+--------+---------------------
Insert | add(e) | offer(e) | put(e) | offer(e, time, unit)
Remove | remove() | poll() | take() | poll(time, unit)
Examine | element() | peek() | N/A | N/A
* https://meta.stackexchange.com/questions/73566
You're confusing Queue with Stack; push and pop are associated with the latter.
Think of Queue in its proper producer/consumer context; poll and offer will make far more sense.