Java Program to Implement ConcurrentLinkedQueue API Last Updated : 24 Jun, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The ConcurrentLinkedQueue class in Java is a part of the Java Collection Framework. It belongs to java.util.concurrent package. It was introduced in JDK 1.5. It is used to implement Queue with the help of LinkedList concurrently. It is an unbounded thread-safe implementation of Queue which inserts elements at the tail of the Queue in a FIFO(first-in-first-out) fashion. It can be used when an unbounded Queue is shared among many threads. To implement ConcurrentLinkedQueue API first we create a class "ConcurrentLinkedQueueImplmentation" and create all the methods of the queue in this class. Implementation of the ConcurrentLinkedQueue API: Java // Java program to demonstrate the // implementation of ConcurrentLinkedQueue API import java.util.Collection; import java.util.Iterator; import java.util.concurrent.ConcurrentLinkedQueue; class ConcurrentLinkedQueueImplmentation<E> { private ConcurrentLinkedQueue<E> concurrentLinkedQueue; // Constructor creates a new empty ConcurrentLinkedQueue public ConcurrentLinkedQueueImplmentation() { // New empty concurrentLinkedQueue concurrentLinkedQueue = new ConcurrentLinkedQueue<>(); } // Constructor creates a new ConcurrentLinkedQueue of // type E public ConcurrentLinkedQueueImplmentation(Collection<? extends E> c) { // New empty concurrentLinkedQueue concurrentLinkedQueue = new ConcurrentLinkedQueue<>(c); } // Add specified element to the tail of the queue public boolean add(E e) { // Add element return concurrentLinkedQueue.add(e); } // Returns true if the queue contains the specified // element public boolean contains(Object o) { return concurrentLinkedQueue.contains(o); } // Returns an iterator over the elements of the queue public Iterator<E> iterator() { return concurrentLinkedQueue.iterator(); } // Add the specified element at the tail of the queue public boolean offer(E e) { return concurrentLinkedQueue.offer(e); } // Returns the peek of the queue or returns null if this // queue is empty. public E peek() { return concurrentLinkedQueue.peek(); } // Retrieves and removes the head of this queue, or // returns null if this queue is empty public E poll() { return concurrentLinkedQueue.poll(); } // Removes a single instance of the specified element // from this queue, if it is present. public boolean remove(Object o) { return concurrentLinkedQueue.remove(o); } // Returns the size of the queue public int size() { return concurrentLinkedQueue.size(); } // Returns an array containing all of the elements in // this queue public Object[] toArray() { return concurrentLinkedQueue.toArray(); } // Returns an array containing all of the elements in // this queue, in proper sequence; the return type of // the array is that of the specified // array. public <T> T[] toArray(T[] a) { return concurrentLinkedQueue.toArray(a); } } public class GFG { public static void main(String[] arg) { // New ConcurrentLinkedQueue ConcurrentLinkedQueueImplmentation<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueueImplmentation< Integer>(); // Adding elements to the ConcurrentLinkedQueue concurrentLinkedQueue.add(10); concurrentLinkedQueue.add(20); concurrentLinkedQueue.add(30); concurrentLinkedQueue.add(40); concurrentLinkedQueue.add(50); System.out.println( "The elements of the ConcurrentLinkedQueue is:"); // Iterator to iterate over ConcurrentLinkedQueue Iterator<Integer> it = concurrentLinkedQueue.iterator(); // Iterate over ConcurrentLinkedQueue while (it.hasNext()) { System.out.print(it.next() + " "); } System.out.println(); // Print the size of the queue System.out.println("Size of the concurrentLinkedQueue is: " + concurrentLinkedQueue.size()); System.out.println(); // Print Peek element of the queue System.out.println("The peek element of the concurrentLinkedQueue is: " + concurrentLinkedQueue.peek()); System.out.println(); // Print the polled element of the queue System.out.println("The polled element of the concurrentLinkedQueue is: " + concurrentLinkedQueue.poll()); System.out.println(); // Remove specified element to the queue, returns // true if removed successfully or returns null if // element not present in the queue System.out.println("Remove 30: " + concurrentLinkedQueue.remove(30)); System.out.println(); // Check whether the spaecified element is present // or not in the queue System.out.println("The concurrentLinkedQueue contains 40:" + concurrentLinkedQueue.contains(40)); System.out.println(); // Print the size of the queue System.out.println("Size of the concurrentLinkedQueue is: " + concurrentLinkedQueue.size()); } } OutputThe elements of the ConcurrentLinkedQueue is: 10 20 30 40 50 Size of the concurrentLinkedQueue is: 5 The peek element of the concurrentLinkedQueue is: 10 The polled element of the concurrentLinkedQueue is: 10 Remove 30: true The concurrentLinkedQueue contains 40:true Size of the concurrentLinkedQueue is: 3 Comment More infoAdvertise with us Next Article Java Program to Implement ConcurrentLinkedQueue API K KapilChhipa Follow Improve Article Tags : Java Java Programs Java-Collections Java-ConcurrentLinkedQueue Java-concurrent-package +1 More Practice Tags : JavaJava-Collections Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Like