ConcurrentLinkedQueue add() method in Java Last Updated : 26 Nov, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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); } } Output: ConcurrentLinkedQueue: [Kolkata, Patna, Delhi, Jammu] Example 2: To demonstrate add() method of ConcurrentLinkedQueue for adding Numbers. Java // 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); } } Output: ConcurrentLinkedQueue: [4353, 7824, 78249, 8724] Example 3: To demonstrate NullPointerException thrown by add() method for adding Null. Java // 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); } } } Output: Exception thrown while adding null: java.lang.NullPointerException Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#add-E- Comment More infoAdvertise with us Next Article ConcurrentLinkedDeque addFirst() method in Java A AmanSingh2210 Follow Improve Article Tags : Java Java-Collections Java - util package java-basics Java-Functions java-queue Java-ConcurrentLinkedQueue +3 More Practice Tags : JavaJava-Collections Similar Reads ConcurrentLinkedQueue addAll() method in Java The addAll() method of ConcurrentLinkedQueue is used to inserts all the elements of the Collection, passed as parameter to this method, at the end of a ConcurrentLinkedQueue. The insertion of element is in same order as returned by the collections iterator. Syntax: public boolean addAll(Collection 4 min read ConcurrentLinkedDeque add() method in Java The java.util.concurrent.ConcurrentLinkedDeque.add() is an in-built function in Java which inserts the specified element at the end of the deque. Syntax: conn_linked_deque.add(elem) Parameter: The method accepts only a single parameter elem which is to be added to tail of the ConcurentLinkedDeque. R 2 min read ConcurrentLinkedDeque addLast() method in Java The java.util.concurrent.ConcurrentLinkedDeque.addLast() is an in-built function in Java which inserts the specified element to the end of the deque. Syntax: conn_linked_deque.addLast(elem) Parameter: The method accepts only a single parameter elem which is to be added to the end of the ConcurrentLi 2 min read ConcurrentLinkedDeque addFirst() method in Java The java.util.concurrent.ConcurrentLinkedDeque.addFirst() is an in-built function in Java which inserts the specified element at the front of the ConcurrentLinkedDeque. Syntax: conn_linked_deque.addFirst(elem) Parameter: The method accepts only a single parameter elem which is to be added to the beg 2 min read ConcurrentLinkedQueue size() method in Java The size() method of ConcurrentLinkedQueue is used to returns number of elements that this ConcurrentLinkedQueue contains. Syntax: public int size() Returns: This method number of elements in this ConcurrentLinkedQueue. Below programs illustrate size() method of ConcurrentLinkedQueue: Example 1: Jav 2 min read ConcurrentLinkedQueue poll() method in Java The poll() method of ConcurrentLinkedQueue is used to remove and return the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method will return null. Syntax: public E poll() Returns: This method remove and returns the head of this ConcurrentLinkedQueue or null if t 2 min read Like