Thursday, December 18, 2025
HomeLanguagesJavaDeque element() method in Java

Deque element() method in Java

The element() method of Deque Interface returns the element at the front the container. It does not deletes the element in the container. This method returns the head of the Deque. The method throws an exception when the Deque is empty. 
Syntax: 
 

E element()

Parameters: This method does not accepts any parameter.
Returns: This method returns the element at the front the container or the head of the Deque.
Exception: The function throws NoSuchElementException when the Deque is empty and the function is called. 
Below programs illustrate element() method of Deque:
Program 1: With the help of LinkedList
 

Java




// Java Program Demonstrate element()
// method of Deque
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of Deque
        Deque<Integer> DQ
            = new LinkedList<Integer>();
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // print Deque
        System.out.println("Deque: " + DQ);
 
        // print head
        System.out.println("Deque's head: " + DQ.element());
    }
}


Output: 

Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

 

Program 2: With the help of ArrayDeque

Java




// Java Program Demonstrate element()
// method of Deque
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of Deque
        Deque<Integer> DQ
            = new ArrayDeque<Integer>();
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // print Deque
        System.out.println("Deque: " + DQ);
 
        // print head
        System.out.println("Deque's head: " + DQ.element());
    }
}


Output: 

Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

 

Program 3: With the help of ConcurrentLinkedDeque

Java




// Java Program Demonstrate element()
// method of Deque
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of Deque
        Deque<Integer> DQ
            = new ConcurrentLinkedDeque<Integer>();
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // print Deque
        System.out.println("Deque: " + DQ);
 
        // print head
        System.out.println("Deque's head: " + DQ.element());
    }
}


Output: 

Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

 

Program 4: With the help of LinkedBlockingDeque

Java




// Java Program Demonstrate element()
// method of Deque
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of Deque
        Deque<Integer> DQ
            = new LinkedBlockingDeque<Integer>();
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // print Deque
        System.out.println("Deque: " + DQ);
 
        // print head
        System.out.println("Deque's head: " + DQ.element());
    }
}


Output: 

Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

 

Program 2: 

Java




// Java Program Demonstrate element()
// method of Deque
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of Deque
        Deque<Integer> DQ
            = new LinkedList<Integer>();
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // print Deque
        System.out.println("Deque: " + DQ);
 
        // print head
        System.out.println("Deque's head: " + DQ.element());
 
        DQ.clear();
 
        // Deque is empty now hence exception
        System.out.println("Deque's head: " + DQ.element());
    }
}


Output: 

Exception in thread "main" java.util.NoSuchElementException
    at java.util.LinkedList.getFirst(LinkedList.java:244)
    at java.util.LinkedList.element(LinkedList.java:663)
    at GFG.main(GFG.java:29)

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#element–

RELATED ARTICLES

Most Popular

Dominic
32455 POSTS0 COMMENTS
Milvus
108 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12036 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6910 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS