LinkedTransferQueue add() method in Java
The add() method of java.util.concurrent.LinkedTransferQueue is an in-built function in Java which is used to insert an element in this queue.
Syntax:
LinkedTransferQueue.add(E e)
Parameters: The function accepts a single parameter e i.e. the element to be inserted.
Return Value: The function returns a boolean value.
Exception: The function throws NullPointerException when the specified element is Null.
Below programs illustrate the use of java.util.concurrent.LinkedTransferQueue.add() :
Program 1: Inserting Integers in the queue.
// Java Program Demonstrate add() // method of LinkedTransferQueue import java.util.concurrent.*; class LinkedTransferQueueAddExample1 { public static void main(String[] args) { // Initializing the queue LinkedTransferQueue<Integer> queue = new LinkedTransferQueue<Integer>(); // Adding elements to this queue for (int i = 10; i <= 15; i++) queue.add(i); // Printing the elements of the queue System.out.println("The elements in the queue are:"); for (Integer i : queue) System.out.print(i + " "); } } |
The elements in the queue are: 10 11 12 13 14 15
Program 2: Adding String in the queue.
// Java Program Demonstrate add() // method of LinkedTransferQueue import java.util.concurrent.*; class LinkedTransferQueueAddExample2 { public static void main(String[] args) { // Initializing the queue LinkedTransferQueue<String> queue = new LinkedTransferQueue<String>(); // Adding elements to this queue queue.add("a"); queue.add("b"); queue.add("c"); queue.add("d"); queue.add("e"); // Printing the elements of the queue System.out.println("The elements in the queue are:"); for (String i : queue) System.out.print(i + " "); } } |
The elements in the queue are: a b c d e
Program 3: To demonstrate NullPointerException
// Java Program Demonstrate add() // method of LinkedTransferQueue import java.util.concurrent.*; class LinkedTransferQueueAddExample2 { public static void main(String[] args) { // Initializing the queue LinkedTransferQueue<String> queue = new LinkedTransferQueue<String>(); try { // Adding null to this queue queue.add(null); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
Exception: java.lang.NullPointerException
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedTransferQueue.html#add(E)
Recommended Posts:
- LinkedTransferQueue put() method in Java
- LinkedTransferQueue take() method in Java
- LinkedTransferQueue contains() method in Java
- LinkedTransferQueue hasWaitingConsumer() method in Java
- LinkedTransferQueue remove() method in Java
- LinkedTransferQueue offer() method in Java
- LinkedTransferQueue drainTo() method in Java
- LinkedTransferQueue size() method in Java
- LinkedTransferQueue iterator() method in Java
- LinkedTransferQueue isEmpty() method in Java
- LinkedTransferQueue spliterator() method in Java
- LinkedTransferQueue peek() method in Java
- LinkedTransferQueue poll() method in Java
- LinkedTransferQueue getWaitingConsumerCount() method in Java with Examples
- LinkedTransferQueue tryTransfer() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



