Home » Java ConcurrentLinkedQueue add() Method with Examples

Java ConcurrentLinkedQueue add() Method with Examples

by Online Tutorials Library

Java ConcurrentLinkedQueue add() method

The add() method of ConcurrentLinkedQueue class inserts the specified element at the tail of this ConcurrentLinkedQueue. The addAll() method overrides the addAll in class AbstractQueue<E>

Syntax

Parameter

e– It is the element to add

Specified By

The add() method of ConcurrentLinkedQueue class is specified by :

  1. add() method in interface Queue<E>.
  2. add() method in interface Collection<E>.

Return Value

The add() method returns a Boolean value true.

Throws

NullPointerException: If the specified element e is null.

Example 1

Test it Now

Output:

After adding 1 to the queue :  [1]  After adding 2 to the queue :  [1, 2]  After adding 3 to the queue :  [1, 2, 3]  After adding 4 to the queue :  [1, 2, 3, 4]  After adding 5 to the queue :  [1, 2, 3, 4, 5]  

Example 2

Test it Now

Output:

Queue :  718  8  18  119  Max number = 718  Min number = 8  

Example 3

Test it Now

Output:

1. Name = Reema  2. Name = Jyoti  3. Name = Rahul  4. Name = Anushtha  

Example 4

Test it Now

Output:

Exception in thread "main" java.lang.NullPointerException  at java.util.concurrent.ConcurrentLinkedQueue.checkNotNull(ConcurrentLinkedQueue.java:920)  at java.util.concurrent.ConcurrentLinkedQueue.offer(ConcurrentLinkedQueue.java:327)  at java.util.concurrent.ConcurrentLinkedQueue.add(ConcurrentLinkedQueue.java:297)  at com.tutorAspire.ConcurrentLinkedQueueAddExample4.main(ConcurrentLinkedQueueAddExample4.java:7)  

If any of the specified elements in this queue is null, it will give NullPointerException as described above.

You may also like