×

Concurrent Linked Deque in Java with Examples

Introduction

Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the deque interfaces.

The Abstract Collection class and the Collection interface are implemented by the Java Concurrent Linked Deque class, which is a component of the Java Collection Framework. The java. util. concurrent package contains it. Concurrently using LinkedList, it is utilized to implement Deque.

When numerous threads need to access the same collection, a concurrent linked deque in Java is a good option since concurrent inserting, deletion, and reading operations run securely throughout many threads. Keep in mind that it doesn't block functions as the Blocking Dequeue interface does.

A component of the Java Collections Framework is this concurrent LinkedList de queue class. effects of memory accuracy. Like other concurrent collections, operations in one thread that precede adding an item to a Concurrent Linked Deque occur before actions in another thread that access or remove that element from the Concurrent Linked Dequeue.

Declaration of Concurrent Linked Dequeue

It complies with the interfaces for Serializable, Iterated, Collection, Deque, and Queue. the declaration syntax for concurrent linked dequeue is as follows

ConcurrentLinkedDequeue < X > cd = new ConcurrentLinkedDequeue < X > ();

Where here, X is the maintained elements

Concurrent Linked Dequeue Constructors

Concurrent linked dequeue implements two distinct kinds of constructors. While some of the methods accept parameters in the form of collections, others only accept an empty dequeue. The concurrent linked de queue is built using the methods listed above.

The constructors implemented by concurrent linked dequeue are

  1. ConcurrentLinkedDequeue ()
  2. ConcurrentLinkedDequeue (collection a)

Example program for concurrent linked dequeue constructor is as follows

Code

// Example Java Program to implement basic Concurrent Linked Deque
  
import java. io. *;
import java. util. *;
import java. util. concurrent. *;
  
class Demo 
{
public static void main (String [] s)
{
          // Declare the Concurrent Linked Deque using empty ConcurrentLinkedDequeue () method
ConcurrentLinkedDeque<String> c = new ConcurrentLinkedDeque<String>();
          // insert items on the front by using add the first method
        c. add First("A");
        c. add First("B");
        c. add First("C");
        c. add First("D");
        c. add First("E");
  
// Concurrent Linked Deque present items are printed
System. out. print (" Concurrent Linked Deque elements == " + c); 


// Now the other concurrent linked dequeue is declared using the collection constructor
 ConcurrentLinkedDeque<String> c1 = new ConcurrentLinkedDeque<String>(c); 


// Concurrent Linked Deque present items are printed 
 System. out. print (" new Concurrent Linked Deque elements are: " + c1);


// now all the elements are printed once again of the existing concurrent linked dequeue
System. out. print (" The elements of Concurrent Linked Deque are == " + c); 


// printing the first element using the peek First () technique
System. out. print ("The First item in the queue is == " + c. peek First ()); 
// get Last () method to print the last element
System. out. print ("The Last item in the queue is == " + c. get Last ());
// remove Last () method to remove the last element       
c. remove Last ();  
System. out. print (“The Last Element removed from the queue”);
// printing the updated Concurrent Linked Deque
System. out. print (" The updated elements of the queue are == " + c);
}
}

Output

C:\java>javac Demo.java
C:\java>java Demo
 Concurrent Linked Deque elements == [E, D, C, B, A]
 new Concurrent Linked Deque elements are: [E, D, C, B, A]
 The elements of Concurrent Linked Deque are == [E, D, C, B, A]
 The first item in the queue is == E 
 The last item in the queue is == A
 The last Element removed from the queue
The updated elements of the queue are == [E, D, C, B]

Operations of Concurrent Linked Deque

Just like other queues the basic functions that are performed on the concurrent linked de queue are inserting the elements, removing the elements, and also access and retrieval of items respectively.

The operations are

  1. Adding
  2. Removing
  3. Accessing
  4. Iterating

Concurrent Linked Deque can be iterated using the descending Iterator () or iterator () methods.

Let’s see an example program for implementing iteration of concurrent linked dequeue

Code

// code for implementing iteration items of Concurrent Linked Deque
import java. io. *;
import java. util. *;
import java. util. concurrent. *;
public class Demo
{
public static void main (String s [])
{
ConcurrentLinkedDeque<Integer> c = new ConcurrentLinkedDeque<Integer>();
                       
c. add (6);
c. add (43);
c. add (11);
c. add (90);
c. add (65);


// prints the concurrent linked dequeue
System. out. println (" The elements present in concurrent linked dequeue are “+ c);


// iterator 1 is created
Iterator it = c. iterator ();


System. out. println ("The iterated items are ");
                       
while (it. has Next ()) 
{
System. out. println (it. next ());
}


// iterator 2 is created to read and print elements in reversing the order             
Iterator it2 = c. descending Iterator ();


System. out. println (" The iterated items in reverse order are ");
while (it2. has Next ()) 
{
System. out. println (it2. next());
}
}
}

Output

C:\java>javac Demo.java


C:\java>java Demo
 The elements present in concurrent linked dequeue are [6, 43, 11, 90, 65]
The iterated items are
6
43
11
90
65
 The iterated items in reverse order are
65
90
11
43
6

Related Topics

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Java String toLowerCase() methods

Java String toLowerCase() method is used to convert all the characters of the String into lower case. Syntax: public String toLowerCase()              public String toLowerCase(Locale locale) Returns: It returns Lower...

1 minute read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

Split String into String Array in Java

The String split() technique returns a variety of divided strings after the strategy parts the given String around matches of a given normal articulation containing the delimiters. The ordinary articulation...

4 minutes read.

Interface in Java

  Interface in Java In Java, the interface is just like a class that has only static constants and abstract methods. It is used to achieve polymorphism so that it can also...

6 minutes read.

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Stack in Java

Java provides a number of collection frameworks to store the collection of objects. Among the collection of data structures " Stack " is one of them. Stack is one of...

5 minutes read.

Cosmic Superclass in Java

The parent class of all Java classes is the Object class. The Java Object class is the parent of all Java classes, whether directly or indirectly. The Object class is...

6 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

Fibonacci Series Program in Java

Fibonacci Series Program in Java using Recursion Fibonacci series is a series whose every term is comprised of adding its previous two terms, barring the first two terms 0 and 1....

3 minutes read.

How to run Java Program in Command Prompt

How to run Java Program in Command Prompt In this section, we will learn how to write, save, compile, and execute or run a Java program in the Command Prompt. Note: One...

3 minutes read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

Java Characters

Normally, when we work with characters, we use primitive data types char. When we have to work with the objects of char, we use Character class. Character class has many important...

2 minutes read.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.