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 IllegalStateException or return false.
Syntax:
public boolean add(E e)
Parameter: This method takes a single parameter e which represents the element to be insert into this ConcurrentLinkedQueue.
Returns: This method returns true after successful insertion of element.
Exception: This method throws NullPointerException if the specified element is null.
Below programs illustrate add() method of ConcurrentLinkedQueue:
Example 1: To demonstrate add() method of ConcurrentLinkedQueue to add String.
// Java Program Demonstrate add() // method of ConcurrentLinkedQueue import java.util.concurrent.*; public class GFG { public static void main(String[] args) { // create an ConcurrentLinkedQueue ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>(); // Add String to queue queue.add("Kolkata"); queue.add("Patna"); queue.add("Delhi"); queue.add("Jammu"); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); } } |
ConcurrentLinkedQueue: [Kolkata, Patna, Delhi, Jammu]
Example 2: To demonstrate add() method of ConcurrentLinkedQueue for adding Numbers.
// Java Program Demonstrate add() // method of ConcurrentLinkedQueue import java.util.concurrent.*; public class GFG { public static void main(String[] args) { // create an ConcurrentLinkedQueue ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>(); // Add Numbers to queue queue.add(4353); queue.add(7824); queue.add(78249); queue.add(8724); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); } } |
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]
Example 3: To demonstrate NullPointerException thrown by add() method for adding Null.
// Java Program Demonstrate add() // method of ConcurrentLinkedQueue import java.util.concurrent.*; public class GFG { public static void main(String[] args) { // create an ConcurrentLinkedQueue ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>(); // Add null to queue try { queue.add(null); } catch (NullPointerException e) { System.out.println("Exception thrown " + "while adding null: " + e); } } } |
Exception thrown while adding null: java.lang.NullPointerException
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#add-E-
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- ConcurrentLinkedQueue contains() method in Java
- ConcurrentLinkedQueue offer() method in Java
- ConcurrentLinkedQueue isEmpty() method in Java
- ConcurrentLinkedQueue iterator() method in Java
- ConcurrentLinkedQueue peek() method in Java
- ConcurrentLinkedQueue poll() method in Java
- ConcurrentLinkedQueue size() method in Java
- ConcurrentLinkedQueue remove() method in Java
- ConcurrentLinkedQueue addAll() method in Java
- ConcurrentLinkedQueue spliterator() method in Java
- ConcurrentLinkedQueue toArray() Method in Java
- ConcurrentLinkedQueue in Java with Examples
- Java.util.ArrayList.add() Method in Java
- DelayQueue add() method in Java with Examples
- ArrayBlockingQueue add() method in Java
- LinkedList add() Method in Java
- HashSet add() Method in Java
- TreeSet add() Method in Java
- PriorityQueue add() Method in Java
- ArrayDeque add() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

