Home

peekFirst

peekFirst is a method of the Deque interface in Java that retrieves, without removing, the element at the front of the deque. It returns the first element or null if the deque is empty, and it does not modify the deque’s contents. This method supports double-ended queue semantics by allowing inspection of the head without dequeuing it.

A key point is how nulls are handled. In implementations that disallow null elements (such as ArrayDeque),

Related methods include peekLast, which retrieves the last element without removing it, and getFirst, which also

Common implementations of Deque that provide peekFirst include ArrayDeque and LinkedList. ArrayDeque is often preferred for

a
null
return
value
indicates
that
the
deque
is
empty.
In
implementations
that
permit
nulls
(such
as
LinkedList),
a
null
return
could
in
principle
reflect
a
real
element
that
is
null,
so
users
should
rely
on
the
empty-check
semantics
or
use
other
methods
if
nulls
are
possible.
returns
the
first
element
but
throws
NoSuchElementException
if
the
deque
is
empty.
Other
removal
and
inspection
methods
include
pollFirst,
removeFirst,
and
pollLast,
each
with
its
own
behavior
on
emptiness.
non-null
elements
due
to
its
performance
characteristics,
while
LinkedList
can
store
null
elements
but
may
complicate
null-based
checks.
peekFirst
is
frequently
used
when
a
program
needs
to
decide
whether
to
process
or
defer
the
head
element
without
altering
the
queue
structure.