Java LinkedTransferQueue add() method

The add() method of LinkedTransferQueue class is used to insert the specified element at the tail of the LinkedTransferQueue. The add() method overrides add in class AbstractQueue<E>.

Syntax:

Parameters:

e - It is the element to add

Specified By:

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

  • add in interface Collection<E>
  • add in interface BlockingQueue<E>
  • add in interface Queue<E>

Return:

This method returns the Boolean value true.

Throws:

NullPointerException: This method will throw if the specified element 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 = Jack
2. Name = Johnny
3. Name = Angelina
4. Name = Russell

Example 4

Test it Now

Output:

Exception in thread "main" java.lang.NullPointerException
	at java.base/java.util.concurrent.LinkedTransferQueue.xfer(Unknown Source)
	at java.base/java.util.concurrent.LinkedTransferQueue.add(Unknown Source)
	at tests.LinkedTransferQueueAddExample4.main(LinkedTransferQueueAddExample4.java:5)





Latest Courses