ConcurrentLinkedQueue offer() method in Java Last Updated : 26 Nov, 2018 Comments Improve Suggest changes Like Article Like Report The offer() method of ConcurrentLinkedQueue is used to insert the element, passed as parameter, at the tail of this ConcurrentLinkedQueue. This method returns True if insertion is successful. ConcurrentLinkedQueue is unbounded, so this method offer() will never returns false. Syntax: public boolean offer(E e) Parameter: This method takes a single parameter e which represents the element we want to 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 offer() method of ConcurrentLinkedQueue: Example 1: To demonstrate offer() method of ConcurrentLinkedQueue to add String. Java // Java Program Demonstrate offer() // 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 using offer() method queue.offer("Aman"); queue.offer("Amar"); queue.offer("Sanjeet"); queue.offer("Rabi"); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); } } Output: ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi] Example 2: To demonstrate offer() method of ConcurrentLinkedQueue for adding Numbers. Java // Java Program Demonstrate offer() // 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 using offer queue.offer(4353); queue.offer(7824); queue.offer(78249); queue.offer(8724); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); } } Output: ConcurrentLinkedQueue: [4353, 7824, 78249, 8724] Example 3: To demonstrate NullPointerException thrown by offer() method for adding Null. Java // Java Program Demonstrate offer() // 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.offer(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#offer-E- Comment More infoAdvertise with us Next Article ConcurrentLinkedQueue offer() method in Java A AmanSingh2210 Follow Improve Article Tags : Misc Java Java-Collections Java - util package java-basics Java-Functions java-queue Java-ConcurrentLinkedQueue +4 More Practice Tags : JavaJava-CollectionsMisc Similar Reads ConcurrentLinkedQueue peek() method in Java The peek() method of ConcurrentLinkedQueue is used to return the head of the ConcurrentLinkedQueue. It retrieves but does not remove, the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method returns null. Syntax: public E peek() Returns: This method returns the 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 ConcurrentLinkedQueue remove() method in Java The remove(Object o) method of ConcurrentLinkedQueue is used to remove a single instance of the specified element from this ConcurrentLinkedQueue, if it is present. This method removes an element e such that o.equals(e) if this ConcurrentLinkedQueue contains one or more such elements. Remove() metho 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 ConcurrentLinkedDeque offerLast() Method in Java The java.util.concurrent.ConcurrentLinkedDeque.offerLast() method is an inbuilt method in Java which inserts the specified element, passed as a parameter, to the end of the deque. Syntax: Conn_Linked_Deque.offerLast(Object elem) Parameters: The method accepts a parameter elem which species the eleme 2 min read ConcurrentLinkedQueue toArray() Method in Java toArray() : The toArray() method of ConcurrentLinkedQueue is used to returns an array of the same elements as that of the ConcurrentLinkedQueue in proper sequence. Basically, it copies all the element from a ConcurrentLinkedQueue to a new array. This method behaves as a bridge between array and Conc 4 min read ConcurrentLinkedDeque offerFirst() method in Java The java.util.concurrent.ConcurrentLinkedDeque.offerFirst() method is an inbuilt method in Java which inserts the specified element, passed as a parameter, in the front of the deque. Syntax: Conn_Linked_Deque.offerFirst(Object elem) Parameters: The method accepts a parameter elem which species the e 2 min read ConcurrentLinkedQueue add() method in Java 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 IllegalS 2 min read ConcurrentLinkedQueue spliterator() method in Java The spliterator() method of ConcurrentLinkedQueue is used to get a Spliterator of the same elements as ConcurrentLinkedQueue. Created Spliterator is weakly consistent. It can be used with Streams in Java 8. Also it can traverse elements individually and in bulk too. Spliterator is better way to trav 2 min read 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 Like