Open In App

ConcurrentLinkedDeque element() method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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

 



Last Updated : 16 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads