Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
ConcurrentLinkedDeque size() method in Java
Next article icon

ConcurrentLinkedDeque in Java

Last Updated : 14 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The ConcurrentLinkedDeque class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. It belongs to java.util.concurrent package. It is used to implement Deque with the help of LinkedList concurrently.

  • Iterators and spliterators are weakly consistent.
  • Concurrent insertion, removal, and access operations execute safely across multiple threads.
  • It does not permit null elements.
  • size() method is not implemented in constant time. Because of the asynchronous nature of these deques, determining the current number of elements requires a traversal of the elements.

Example: This example demonstrates the usage of ConcurrentLinkedDeque to add and remove elements from both ends of the deque in a thread-safe manner.

Java
// Java Program to dmeonstrates the  // working of ConcurrentLinkedDeque import java.util.Deque; import java.util.concurrent.ConcurrentLinkedDeque;  public class Geeks {     public static void main(String[] args)     {         Deque<Integer> d = new ConcurrentLinkedDeque<>();         d.addFirst(1);         d.addLast(2);         int f = d.pollFirst();         int l = d.pollLast();         System.out.println("First: " + f + ", Last: " + l);     } } 

Output
First: 1, Last: 2 

The ConcurrentLinkedDeque class in Java is a thread-safe implementation of the Deque interface that uses a linked list to store its elements. The ConcurrentLinkedDeque class provides a scalable and high-performance alternative to the ArrayDeque class, particularly in scenarios where multiple threads access the deque concurrently. The ConcurrentLinkedDeque class provides methods for inserting and removing elements from both ends of the deque, and for retrieving elements from the head and tail of the deque, making it a good choice for scenarios where you need to perform many add and remove operations.

ConcurrentLinkedDeque Hierarchy

The below diagram demonstrates the class and interface hierarchy of ConcurrentLinkedDeque showing how it extends AbstractCollecrtion, implements Deque and is a subclass of Queue, and also implements the Serializable interface

ConcurrentLinkedDequeinJava


Declaration of ConcurrentLinkedDeque

In Java, the declaration of ConcurrentLinkedDeque can be done as:

ConcurrentLinkedDeque<Type> deque = new ConcurrentLinkedDeque<>();

Note: Here Type represents the type of element we want to store in the deque(e.g. Integer, String)

Constructors

Constructors

Description

ConcurrentLinkedDeque()

This constructor is used to construct an empty deque.

ConcurrentLinkedDeque(Collection<E> c)

This constructor is used to construct a deque with the elements of the Collection passed as the parameter


Example: This example demonstrates how to create a ConcurrentLinkedDeque, add elements to the front, and create a copy of the deque.

Java
// Java Program to demonstrates  // the working of addFirst() import java.util.concurrent.*;  class Geeks {     public static void main(String[] args) {                  // Create a ConcurrentLinkedDeque         ConcurrentLinkedDeque<Integer> d          = new ConcurrentLinkedDeque<>();          // Add elements to the front         d.addFirst(10);         d.addFirst(20);         d.addFirst(30);         d.addFirst(40);          // Display the deque         System.out.println("Deque: " + d);          // Create another ConcurrentLinkedDeque          // by copying the first one         ConcurrentLinkedDeque<Integer> d1          = new ConcurrentLinkedDeque<>(d);          System.out.println("Deque Copy: " + d1);     } } 

Output
Deque: [40, 30, 20, 10] Deque Copy: [40, 30, 20, 10] 


Example: This example demonstrates how to add elements to both ends of a ConcurrentLinkedDeque, access the first and last elements, and remove elements from both ends in a thread-safe manner.

Java
// Java Program to demonstrates  // the working of addFirst(), // peekLast(), peekFirst(),  // removeLast(), removeFirst() import java.util.concurrent.*; class Geeks {          public static void main(String[] args)     {         // Create a ConcurrentLinkedDeque         ConcurrentLinkedDeque<Integer> d             = new ConcurrentLinkedDeque<>();          // Add elements to the front         d.addFirst(10);         d.addFirst(20);         d.addFirst(30);         d.addFirst(40);          // Display the d         System.out.println("Deque: " + d);          // Display the last element using peekLast()         System.out.println("Last Element: " + d.peekLast());          // Display the first element using peekFirst()         System.out.println("First Element: "                            + d.peekFirst());          // Remove the last element using removeLast()         d.removeLast();          // Display the d after removing the last element         System.out.println(             "Deque after removal of last element: " + d);          // Remove the first element using removeFirst()         d.removeFirst();          // Display the d after removing the first element         System.out.println(             "Deque after removal of first element: " + d);     } } 

Output
Deque: [40, 30, 20, 10] Last Element: 10 First Element: 40 Deque after removal of last element: [40, 30, 20] Deque after removal of first element: [30, 20] 

Performing Various Operations on ConcurrentLinkedDeque

1. Adding Elements: We can use methods like add(), addAll(), addFirst(), addLast() to insert elements to the ConcurrentLinkedDeque.

Example: This example demonstrates how to add elements to both end of a ConcurrentLinkedDeque and copy all elements from one deque to another.

Java
// Add Elements in ConcurrentLinkedDeque import java.util.concurrent.*; class Geeks {      public static void main(String[] args) {                  // Create ConcurrentLinkedDeque d         ConcurrentLinkedDeque<Integer> d          = new ConcurrentLinkedDeque<>();          // Add elements to the tail         d.add(10);         d.add(20);          // Add element to the head         d.addFirst(30);          // Display the elements in d         System.out.println("Elements in d: " + d);          // Create another ConcurrentLinkedDeque d1         ConcurrentLinkedDeque<Integer> d1          = new ConcurrentLinkedDeque<>();          // Add all elements from d to d1         d1.addAll(d);          // Display the elements in cld2         System.out.println("Elements in d1: " + d1);     } } 

Output
Elements in d: [30, 10, 20] Elements in d1: [30, 10, 20] 


2. Removing Elements: We can use methods like remove(), remove(Object), removeFirst(), removeLast() to remove elements from the ConcurrentLinkedDeque.

Example:

Java
// Java Program to demosntrates the  // working of remove(), remove(Object),  // removeFirst(), removeLast() import java.util.concurrent.*; class Geeks {          public static void main(String[] args) {          // Create ConcurrentLinkedDeque d         ConcurrentLinkedDeque<Integer> d          = new ConcurrentLinkedDeque<>();          // Add elements to the deque         d.add(40);         d.add(50);         d.add(60);         d.add(70);         d.add(80);          // Display the current deque         System.out.println("Deque: " + d);          // Remove the first element         System.out.println("Removing the first element: "          + d.remove());          // Remove element 60 using remove(Object)         System.out.println("60 removed?: " + d.remove(60));          // Display the deque after removals         System.out.println("Deque after removal: " + d);          // Remove the first element         d.removeFirst();          // Remove the last element         d.removeLast();          // Display the deque after removing         // first and last elements         System.out.println("Deque after removing first and last element: "          + d);     } } 

Output
Deque: [40, 50, 60, 70, 80] Removing the first element: 40 60 removed?: true Deque after removal: [50, 70, 80] Deque after removing first and last element: [70] 


3. Iterating Elements: We can use the iterator() or descendingIterator() method to iterate over the elements of the ConcurrentLinkedDeque.

Example:

Java
// Java Program to demonstrates  // the working of iterator() import java.util.concurrent.*; import java.util.*;  public class Geeks {      public static void main(String args[]) {          // Create an empty ConcurrentLinkedDeque         ConcurrentLinkedDeque<String> d          = new ConcurrentLinkedDeque<>();          // Add elements to the d         d.add("Java");         d.add("C++");         d.add("Python");         d.add("Js");          // Display the d         System.out.println("Deque: " + d);          // Create an iterator for the d         Iterator<String> i = d.iterator();          // Display the elements using the iterator         System.out.println("Iterating over the elements of ConcurrentLinkedDeque: ");         while (i.hasNext()) {             System.out.println(i.next());         }     } } 

Output
Deque: [Java, C++, Python, Js] Iterating over the elements of ConcurrentLinkedDeque:  Java C++ Python Js 


4. Accessing Elements: We can use methods like getFirst(), getLast(), element() to access the elements of the ConcurrentLinkedDeque.

Example:

Java
// Java Program to demonstrates  // the working og getFirst(), // getLast() and element() import java.util.*; import java.util.concurrent.*; class Geeks {     public static void main(String[] args)     {         // Create an empty ConcurrentLinkedDeque         ConcurrentLinkedDeque<String> d             = new ConcurrentLinkedDeque<>();          // Add elements to the d         d.add("Java");         d.add("C++");         d.add("Python");         d.add("Js");          // Display the elements in the d         System.out.println("Deque: " + d);          // Display the first element         System.out.println("First element: "                            + d.getFirst());          // Display the last element         System.out.println("Last element: " + d.getLast());          // Display the head of the d         System.out.println("Head of deque: "          + d.element());     } } 

Output
Deque: [Java, C++, Python, Js] First element: Java Last element: Js Head of deque: Java 

Methods

Here, E is the type of element.

Methods

Description

add​(E e)Inserts the specified element at the tail of this deque.
addAll​(Collection<? extends E> c)Appends all of the elements in the specified collection to the end of this deque, in the order that they are returned by the specified collection’s iterator.
addFirst​(E e)Inserts the specified element at the front of this deque.
addLast​(E e)Inserts the specified element at the end of this deque.
clear()Removes all of the elements from this deque.
contains​(Object o)Returns true if this deque contains the specified element.
descendingIterator()Returns an iterator over the elements in this deque in reverse sequential order.
element()Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque).
forEach​(Consumer<? super E> action)Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
getFirst()Retrieves, but does not remove, the first element of this deque.
getLast()Retrieves, but does not remove, the last element of this deque.
isEmpty()Returns true if this collection contains no elements.
iterator()Returns an iterator over the elements in this deque in the proper sequence.
offer​(E e)Inserts the specified element at the tail of this deque.
offerFirst​(E e)Inserts the specified element at the front of this deque.
offerLast​(E e)Inserts the specified element at the end of this deque.
pop()Pops an element from the stack represented by this deque.
push​(E e)Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.
remove()Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque).
remove​(Object o)Removes the first occurrence of the specified element from this deque.
removeAll​(Collection<?> c)Removes all of this collection’s elements that are also contained in the specified collection (optional operation).
removeFirst()Retrieves and removes the first element of this deque.
removeFirstOccurrence​(Object o)Removes the first occurrence of the specified element from this deque.
removeIf​(Predicate<? super E> filter)Removes all of the elements of this collection that satisfy the given predicate.
removeLast()Retrieves and removes the last element of this deque.
removeLastOccurrence​(Object o)Removes the last occurrence of the specified element from this deque.
retainAll​(Collection<?> c)Retains only the elements in this collection that are contained in the specified collection (optional operation).
size()Returns the number of elements in this deque.
spliterator()Returns a Spliterator over the elements in this deque.
toArray()Returns an array containing all of the elements in this deque, in proper sequence (from first to last element).
toArray​(T[] a)Returns an array containing all of the elements in this deque, in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.


Methods Declared in Class java.util.AbstractCollection

Method

Description

containsAll​(Collection<?> c)Returns true if this collection contains all of the elements in the specified collection.
toString()Returns a string representation of this collection.


Methods Declared in Interface java.util.Collection

Method

Description

containsAll​(Collection<?> c)Returns true if this collection contains all of the elements in the specified collection.
equals​(Object o)Compares the specified object with this collection for equality.
hashCode()Returns the hash code value for this collection.
parallelStream()Returns a possibly parallel Stream with this collection as its source.
stream()Returns a sequential Stream with this collection as its source.
toArray​(IntFunction<T[]> generator)Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.


Methods Declared in Interface java.util.Deque

Method

Description

peek()Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.
peekFirst()Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
peekLast()Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
poll()Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.
pollFirst()Retrieves and removes the first element of this deque, or returns null if this deque is empty.
pollLast()Retrieves and removes the last element of this deque, or returns null if this deque is empty.




Next Article
ConcurrentLinkedDeque size() method in Java

R

RishabhPrabhu
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java-ConcurrentLinkedDeque
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • ConcurrentLinkedQueue in Java
    In Java, the ConcurrentLinkedQueue is the part of the java.util.concurrent package and implements a FIFO(First-In-First-Out) queue. It is a thread-safe, non-blocking, and scalable queue designed for use in highly concurrent environments. The queue uses a lock-free algorithm, ensuring that multiple t
    8 min read
  • ConcurrentLinkedDeque add() method in Java
    The java.util.concurrent.ConcurrentLinkedDeque.add() is an in-built function in Java which inserts the specified element at the end of the deque. Syntax: conn_linked_deque.add(elem) Parameter: The method accepts only a single parameter elem which is to be added to tail of the ConcurentLinkedDeque. R
    2 min read
  • ConcurrentLinkedDeque size() method in Java
    The size() method of ConcurrentLinkedDeque class in Java is used to find the number of elements present in the ConcurrentLinkedDeque container. In other words, this method tells the current capacity of the container. The value returned by this method is of integral type and in case if the container
    2 min read
  • ConcurrentLinkedDeque clear() method in Java
    The java.util.concurrent.ConcurrentLinkedDeque.clear() method is an inbuilt method in Java which removes the elements in the Deque. Syntax: public void clear() Parameters: The method do not accepts any parameter. Return Value: The function does not return anything Below programs illustrate the Concu
    2 min read
  • 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 metho
    2 min read
  • ConcurrentLinkedDeque addLast() method in Java
    The java.util.concurrent.ConcurrentLinkedDeque.addLast() is an in-built function in Java which inserts the specified element to the end of the deque. Syntax: conn_linked_deque.addLast(elem) Parameter: The method accepts only a single parameter elem which is to be added to the end of the ConcurrentLi
    2 min read
  • ConcurrentLinkedDeque getLast() Method in Java
    The java.util.concurrent.ConcurrentLinkedDeque.getLast() method is an in-built method in Java which returns the last element of the deque container. Syntax: Conn_Linked_Deque.getLast() Parameters: The method does not accept any parameter. Return Value: The method returns the last element present in
    2 min read
  • ConcurrentLinkedDeque pollLast() method in Java
    The java.util.concurrent.ConcurrentLinkedDeque.pollLast() is an in-built method in Java which retrieves the last element of this deque and removes it. If the deque is empty, the method returns NULL. Syntax: Conn_Linked_Deque.pollLast() Parameters: The function accepts no parameters. Return Values:Th
    2 min read
  • ConcurrentLinkedQueue add() method in Java
    The add() method of ConcurrentLinkedQueue is used to insert the element, passed as parameter to add() of ConcurrentLinkedQueue, at the tail of this ConcurrentLinkedQueue. This method returns True if insertion is successful. ConcurrentLinkedQueue is unbounded, so this method will never throw IllegalS
    2 min read
  • ConcurrentLinkedDeque addFirst() method in Java
    The java.util.concurrent.ConcurrentLinkedDeque.addFirst() is an in-built function in Java which inserts the specified element at the front of the ConcurrentLinkedDeque. Syntax: conn_linked_deque.addFirst(elem) Parameter: The method accepts only a single parameter elem which is to be added to the beg
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences