VOOZH about

URL: https://www.geeksforgeeks.org/java/concurrentlinkeddeque-in-java-with-examples/

⇱ ConcurrentLinkedDeque in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

ConcurrentLinkedDeque in Java

Last Updated : 11 Jul, 2025

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.


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.


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.


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.


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:


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:


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:


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.


Comment