Wednesday, July 3, 2024
HomeLanguagesJavaBlockingDeque element() method in java with examples

BlockingDeque element() method in java with examples

The element() method of BlockingDeque returns the element at the front the container. It does not delete the element in the container. This method returns the head of the queue represented by this deque.

Syntax:  

public void element()

Parameters: This method does not accept any parameter.

Returns: This method returns the head of the queue represented by this deque.

Note: The element() method of BlockingDeque has been inherited from the LinkedBlockingDeque class in Java. 

Below programs illustrate element() method of BlockingDeque:

Program 1:  

Java




// Java Program Demonstrate element()
// method of BlockingDeque
 
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of BlockingDeque
        BlockingDeque<Integer> BD
            = new LinkedBlockingDeque<Integer>();
 
        // Add numbers to end of BlockingDeque
        BD.add(10);
        BD.add(20);
        BD.add(30);
        BD.add(40);
 
        // before removing print Deque
        System.out.println("Blocking Deque: " + BD);
 
        System.out.println("Blocking Deque front element: " + BD.element());
    }
}


Output: 

Blocking Deque: [10, 20, 30, 40]
Blocking Deque front element: 10

 

Program 2: 

Java




// Java Program Demonstrate element()
// method of BlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
import java.util.concurrent.BlockingDeque;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of BlockingDeque
        BlockingDeque<String> BD
            = new LinkedBlockingDeque<String>();
 
        // Add numbers to end of BlockingDeque
        BD.add("ab");
        BD.add("cd");
        BD.add("fg");
        BD.add("xz");
 
        // before removing print Deque
        System.out.println("Blocking Deque: " + BD);
 
        System.out.println("Blocking Deque front element: " + BD.element());
    }
}


Output: 

Blocking Deque: [ab, cd, fg, xz]
Blocking Deque front element: ab

 

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#element()
 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments