38

The Queue implementation in Java has two methods to remove element, One is remove() which throws exception and other one is poll() which returns null for an empty queue. I have two doubts:

  1. Why Queue has different implementation to remove element?
  2. Which implementation to use When?
palacsint
  • 28,416
  • 10
  • 82
  • 109
munish
  • 389
  • 1
  • 3
  • 3

7 Answers7

34

In some situations it's expected that a queue will be empty, and in those cases having a method that doesn't throw an exception is appropriate. In other situations it's an exceptional circumstance that the queue is empty, and an exception is appropriate.

Throwing exceptions incurs a performance penalty, and if it's the case that you expect the queue to be empty from time to time you don't want to have to handle the queue-empty-logic as catching an exception -- it's both costly and difficult to read.

In the opposite case where you don't expect the queue to ever be empty it is a sign of a programming error, or some other exceptional circumstance that it is, and you don't want to write ugly error condition checking code (e.g. checking for null), because in this case that would be less readable than catching an exception (which you can do in another scope).

Theo
  • 131,503
  • 21
  • 160
  • 205
  • 4
    This answer explains the basic difference,just fails to tell,which throws the exception,and which doesn't. – Achow Dec 21 '12 at 08:05
  • 1
    `add()` and `remove()` throws an Exception, while `offer()` and `poll()` does not. – gokcand Jul 23 '19 at 21:58
18

The abstract class AbstractQueue<E> implements Queue<E> and define the remove method.

You can take a look at source code:

public E remove() {
    E x = poll();
    if (x != null)
        return x;
    else
        throw new NoSuchElementException();
}

So, as you can see, the remove() method use poll() method.

You can use which one you prefer.

palacsint
  • 28,416
  • 10
  • 82
  • 109
Massimo Fazzolari
  • 5,185
  • 3
  • 27
  • 36
6

Remove() method differs from poll only in that it throws an exception if this queue is empty.

enter image description here

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
David Wang
  • 96
  • 1
  • 2
5

Looking at the answers, it wasn't clear to me which did what,hence:

Straight from the API : The remove() and poll() methods differ only in their behavior when the queue is empty: the remove() method throws an exception, while the poll() method returns null

Achow
  • 8,600
  • 6
  • 39
  • 49
4

When you know how to react right now and/or expect elements to be absent then use poll.

Otherwise use remove.

Mykola Golubyev
  • 57,943
  • 15
  • 89
  • 102
1

Sometimes you want a null value returned for an empty queue and sometimes you want it to treat an empty queue as a exception case.

Poindexter
  • 2,396
  • 16
  • 19
0

The two methods are used differently in classic discussions about a Queue structure. I use poll() mostly to retrieve items, and remove() mostly if I need to modify the Queue outside of the normal loop.

Jon
  • 1,249
  • 1
  • 10
  • 21