ConcurrentLinkedDeque element() method in Java

The java.util.concurrent.ConcurrentLinkedDeque.element() is an in-built function in java which retrieves but does not remove the head of the queue represented by deque i.e the first element of deque.

Syntax:

conn_linked_deque.element()

Parameter: This method has no parameters.



Return Value: This method returns the first element in the deque.

Exception: This method will throw NoSuchElementException if the deque is empty.

Below programs illustrate the ConcurrentLinkedDeque.element() method:

Program 1: This program involves a ConcurrentLinkedDeque of Integer type.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java example illustrating
// ConcurrentLinkedDeque element() method
  
import java.util.concurrent.*;
  
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
  
        // Create a ConcurrentLinkedDeque
        // using ConcurrentLinkedDeque() contructor
        ConcurrentLinkedDeque<Integer>
            cld = new ConcurrentLinkedDeque<Integer>();
  
        cld.add(12);
        cld.add(70);
        cld.add(1009);
        cld.add(475);
  
        // Displaying the existing LinkedDeque
        System.out.println("ConcurrentLinkedDeque: "
                           + cld);
  
        // Displaying the head of deque
        System.out.println("The Head of deque is: "
                           + cld.element());
    }
}

chevron_right


Output:

ConcurrentLinkedDeque: [12, 70, 1009, 475]
The Head of deque is: 12

Program 2: This program involves a ConcurrentLinkedDeque of String type.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java example illustrating
// ConcurrentLinkedDeque element() method
  
import java.util.concurrent.*;
  
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
  
        // Create a ConcurrentLinkedDeque
        // using ConcurrentLinkedDeque() contructor
        ConcurrentLinkedDeque<String>
            cld = new ConcurrentLinkedDeque<String>();
  
        cld.add("Gfg");
        cld.add("Geeks");
        cld.add("Programming");
        cld.add("contribute");
  
        // Displaying the existing LinkedDeque
        System.out.println("ConcurrentLinkedDeque: "
                           + cld);
  
        // Displaying the head of deque
        System.out.println("The Head of deque is: "
                           + cld.element());
    }
}

chevron_right


Output:

ConcurrentLinkedDeque: [Gfg, Geeks, Programming, contribute]
The Head of deque is: Gfg


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.