How To Peek From A Queue In Java?

Use `queue.peek()` to retrieve the head element without removing it

Use `queue.element()` to retrieve the head element without removing it

`peek()` returns `null` if the queue is empty

`element()` throws `NoSuchElementException` if the queue is empty

Example: `Queue queue = new LinkedList<>();`

Example: `Integer first = queue.peek();`

Suggested for You

Trending Today