Open In App

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.
Output:
ConcurrentLinkedQueue: [Kolkata, Patna, Delhi, Jammu]
Example 2: To demonstrate add() method of ConcurrentLinkedQueue for adding Numbers.
Output:
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]
Example 3: To demonstrate NullPointerException thrown by add() method for adding Null.
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-

Similar Reads