Home

pollFirst

pollFirst is a method in the Java standard library’s Deque interface. It retrieves and removes the first element of the deque, or returns null if the deque is empty. The operation is non-blocking and does not throw an exception when the deque is empty.

In terms of behavior, pollFirst returns the element at the front of the deque and removes it.

pollFirst is effectively the Deque-specific equivalent of poll() when used through the Deque interface, both of

Common usage involves non-blocking queue operations in single- or multi-threaded contexts. For example, a producer-consumer setup

If
the
deque
has
no
elements,
it
returns
null.
This
differs
from
removeFirst,
which
also
removes
the
first
element
but
throws
a
NoSuchElementException
if
the
deque
is
empty.
The
method
is
typically
implemented
with
constant
time
complexity
(O(1))
in
common
deque
implementations
such
as
ArrayDeque
and
LinkedList.
which
remove
and
return
the
head
of
the
deque.
There
is
a
complementary
pollLast()
method
that
removes
and
returns
the
last
element,
or
null
if
empty.
For
blocking
variants,
the
BlockingDeque
interface
provides
takeFirst()
and
related
methods
that
block
until
an
element
becomes
available.
may
use
pollFirst()
to
fetch
the
next
task
from
the
front
of
a
shared
deque
without
risking
an
exception
if
the
deque
is
temporarily
empty.
pollFirst
is
available
in
many
deque
implementations,
such
as
ArrayDeque,
LinkedList,
and
ConcurrentLinkedDeque,
and
is
part
of
the
standard
Java
collections
framework
since
Java
6.