ConcurrentLinkedQueue offer() method in Java
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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.
ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi]
Example 2: To demonstrate offer() method of ConcurrentLinkedQueue for adding Numbers.
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]
Example 3: To demonstrate NullPointerException thrown by offer() method for adding Null.
Exception thrown while adding null: java.lang.NullPointerException