In Java, containers play a crucial role in containing and organizing the data efficiently. The queue is one such container and a class that allows the programmer to apply multiple operations on the queue such as inserting, removing the elements, or iterating through them and checking for the required condition with the help of the queue’s built-in methods.
This tutorial will discuss the usage of the Java “ConcurrentLinkedQueue” class.
How to Use ConcurrentLinkedQueue in Java with Examples?
The “ConcurrentLinkedQueue” class is contained in the “java.util.concurrent” package and is utilized to implement Queue using LinkedList. This class is an implementation of the queue that inserts elements at the end of the queue in a FIFO(first-in-first-out) manner.
“ConcurrentLinkedQueue” Class Methods
Method | Functionality |
add() | It adds the target item at the end of the queue. |
addAll() | It adds/appends all the items in the collection at the last of the queue. |
forEach() | This method applies the target action for each item. |
contains() | It returns true if this queue comprises the target item. |
iterator() | It retrieves an iterator over the items in the queue. |
isEmpty() | It gives true if this queue does not comprise any items. |
offer() | It appends the target item at the last of the queue. |
remove() | This method removes the target item from the queue if contained. |
removeIf() | It removes all the items from the queue that satisfy the condition. |
removeAll() | It removes all the items in the queue which are contained in the particular collection. |
retainAll() | It retains only those items in this queue that are contained in the target collection. |
spliterator() | This method gives a spliterator over the items in the queue. |
toArray() | It returns an array comprising all the items of this queue in proper sequence. |
size() | It gives the total number of items in the queue. |
Example 1: Utilization of the “ConcurrentLinkedQueue” Class in Java
This example utilizes the discussed class to insert the elements in the queue via the combined “for” loop and the class “offer()” method:
import java.util.concurrent.ConcurrentLinkedQueue;
public class Concurrentlinkedqueue {
public static void main(String[] args) {
ConcurrentLinkedQueue<Integer> x = new ConcurrentLinkedQueue<Integer>();
for (int i = 1; i <=3; i++) {
x.add(i);
}
System.out.println("Queue Before Element's Insertion -> "+x);
x.offer(4);
System.out.println("Queue After Element's Insertion -> "+x);
System.out.println(x.contains(4));
System.out.println (x.size());
System.out.println (x.isEmpty());
}}
In these code lines:
- Firstly, import the stated package to make use of the “ConcurrentLinkedQueue” class.
- Create a ConcurrentLinkedQueue object using the “new” keyword and the “ConcurrentLinkedQueue()” constructor.
- After that, apply the “for” loop to add the integers from “1” to “3” in the queue via the “add()” method.
- Now, associate the class “offer()” method to add the target item i.e., “4” at the end/last of the queue.
- Lastly, return the size of the queue and check if it is empty via the “size()” and “isEmpty()” methods, respectively.
Output

Example 2: Applying the “ConcurrentLinkedQueue” Class to Add and Remove Elements From Queue in Java
In this specific example, the stated class’s methods will be utilized to add and remove the elements from the queue:
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.*;
public class Concurrentlinkedqueue {
public static void main(String[] args) {
ConcurrentLinkedQueue<Integer> x = new ConcurrentLinkedQueue<Integer>();
List<Integer> lt = new ArrayList<Integer>();
x.add(1);
x.add(2);
x.add(3);
x.add(4);
lt.add(4);
lt.add(5);
System.out.println("Queue Before Elements Removal -> "+x);
x.remove(4);
System.out.println("Queue After Elements Removal -> "+x);
System.out.println("List Elements -> "+lt);
x.removeAll(lt);
System.out.println("After removal of Queue elements contained in list -> "+x);
}}
In this code:
- Include an additional “java.util.*” package to have access to all the features in the “java.util” package.
- Now, likewise, create a “ConcurrentLinkedQueue” class object.
- Create an ArrayList of “Integer” type.
- Add the stated integers in the queue as well as in the list using the “add()” method.
- Remove the specified element from the queue via the “remove()” method and retrieve the queue.
- Also, display the list elements.
- Lastly, apply the “removeAll()” method to omit all the items in the queue that are contained in the list and display the resultant queue.
Output

Conclusion
The “ConcurrentLinkedQueue” class is contained in the “java.util.concurrent” package and is utilized to implement a queue. This class includes several methods such as “add”, “remove()”, “offer()” etc for carrying out operations on the queue. This blog discussed using and implementing the Java “ConcurrentLinkedQueue” class.