Skip to content
Related Articles

Related Articles

ConcurrentLinkedDeque element() method in Java
  • Last Updated : 16 Apr, 2021
GeeksforGeeks - Summer Carnival Banner

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.
 

JAVA




// Java example illustrating
// ConcurrentLinkedDeque element() method
 
import java.util.concurrent.*;
 
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
 
        // Create a ConcurrentLinkedDeque
        // using ConcurrentLinkedDeque() constructor
        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());
    }
}
Output: 
ConcurrentLinkedDeque: [12, 70, 1009, 475]
The Head of deque is: 12

 

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

JAVA




// Java example illustrating
// ConcurrentLinkedDeque element() method
 
import java.util.concurrent.*;
 
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
 
        // Create a ConcurrentLinkedDeque
        // using ConcurrentLinkedDeque() constructor
        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());
    }
}
Output: 
ConcurrentLinkedDeque: [Gfg, Geeks, Programming, contribute]
The Head of deque is: Gfg

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :