47

Ok, so I've been using Java for a long time now and have recently been preparing for my OCJP exam. I was wondering if anyone might be able to provide any insight into why the method names "poll" (as opposed to the more traditional "pop") and "offer" (as opposed to the more traditional "push") were chosen? I'm looking specifically at the java.util.Queue interface, but would be interested in more general explanations as well :)

This is really more of an academic question than for any specific coding scenario, as I'm just trying to make sense of why Sun (as this was done before Oracle bought them) would choose to the names that they did.

Oh and before anyone decides to crucify me or throw back links to lmgtfy... I've already looked on google, yahoo, wiki, bing, and SO so if I'm overlooking some obvious search criteria or missed some old post here that explains it then I apologize in advance.

casperOne
  • 73,706
  • 19
  • 184
  • 253
zpangwin
  • 1,082
  • 1
  • 12
  • 17
  • 3
    Queues CAN be empty. A pop operation on an empty stack doesn't make much sense, so it makes more sense to call it "Poll" - fetch something if it's available, otherwise do nothing. – Marc B Feb 18 '12 at 16:54
  • 1
    That makes more sense... I had been comparing mostly against Stack and the C++ std classes for stack and queue (not to mention arrays in JS, lol). Between duffymo, Tomasz, and ruakh's answers -- and looking over the api descriptions a little more thoroughly -- I think I see why they chose to avoid the names "push" and "pop" for Queue. Thanks to all for the great responses! – zpangwin Feb 18 '12 at 17:41

5 Answers5

54

Because these methods have different semantics explained in the JavaDoc. add/remove are unconditional while offer/poll return special value:

  • offer only offers a new value, but it might not be accepted, e.g. if the queue is full

  • poll only 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

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
11

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 13
    I don't completely agree. True, `push` and `pop` are very strongly associated with stacks, but they're often used with queues as well; for example, C++'s `std::queue` has `push` and `pop`. – ruakh Feb 18 '12 at 17:01
  • @ruakh I think he mentioned about Java ie no `pop` and `push` for the `Stack` data structure. The question is about Java anyway – Arefe Jul 26 '18 at 07:54
8

The Queue interface defines some methods for acting on the first element of the list, which differ in the way they behave. These methods are:

peek()
element()
poll()
remove()

The peek() This method retrieves the value of the first element of the queue without removing it from the queue. For each invocation of the method we always get the same value and its execution does not affect the size of the queue. If the queue is empty the peek() method returns null.

The element() This method behaves like peek(), so it again retrieves the value of the first element without removing it. however, if the list is empty element() throws a NoSuchElementException.

The poll() This method retrieves the value of the first element of the queue by removing it from the queue. . At each invocation it removes the first element of the list and if the list is already empty it returns null but does not throw any exception.

The remove() This method behaves as the poll() method, so it removes the first element of the list and if the list is empty it throws a NoSuchElementException

dinesh kandpal
  • 738
  • 7
  • 16
2

I assume it's simply because the names push and pop sound ambiguous between the behavior of add and remove and that of offer and poll — and pop, in particular, is reminiscent of java.util.Stack.pop(), which behaves like remove rather than like poll. The names add and remove were obviously chosen to match the names of similar methods elsewhere in the Java Collections Framework, which either perform the requested operation or else raise an exception; the names offer and poll are suggestive of the fact that they don't raise exceptions.

ruakh
  • 175,680
  • 26
  • 273
  • 307
0

All method names have specific usecase may be thats why name

add()

  • Inserts the specified element into the queue. If the task is successful, add() returns true, if not it throws an exception.

offer()

  • Inserts the specified element into the queue. If the task is successful, offer() returns true, if not it returns false.

element()

  • Returns the head of the queue. Throws an exception if the queue is empty.

peek()

  • Returns the head of the queue. Returns null if the queue is empty.

remove()

  • Returns and removes the head of the queue. Throws an exception if the queue is empty.

poll()

  • Returns and removes the head of the queue. Returns null if the queue is empty.
Raj Saraogi
  • 1,780
  • 1
  • 13
  • 21