Java .element()

Anonymous contributor's avatar
Anonymous contributor
Published Aug 7, 2025
Contribute to Docs

In Java, the .element() method of the Queue interface retrieves, but does not remove, the head of the queue. Unlike .peek(), which returns null if the queue is empty, .element() throws a NoSuchElementException in such cases. This method provides a way to inspect the next element to be removed without modifying the queue’s structure.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

Syntax

queueName.element()

Parameters:

The .element() method does not take any parameters.

Return value:

  • Returns the head element of the queue.
  • Throws NoSuchElementException if the queue is empty.

Example

This example demonstrates the .element() method with a LinkedList implementation of Queue:

import java.util.LinkedList;
import java.util.Queue;
import java.util.NoSuchElementException;
public class Main {
public static void main(String[] args) {
Queue<String> animals = new LinkedList<String>();
// Add elements to the queue
animals.offer("Dog");
animals.offer("Cat");
animals.offer("Bird");
// Use .element() to peek at the head without removing it
System.out.println("Head element: " + animals.element());
System.out.println("Queue after element(): " + animals);
// Remove elements and check head
animals.poll();
System.out.println("Head after poll: " + animals.element());
// Clear the queue
animals.clear();
// Attempting to use .element() on empty queue throws exception
try {
animals.element();
} catch (NoSuchElementException e) {
System.out.println("Exception: Queue is empty!");
}
}
}

Here is the output:

Head element: Dog
Queue after element(): [Dog, Cat, Bird]
Head after poll: Cat
Exception: Queue is empty!

All contributors

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours