Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
This article will discuss the Linked Transfer Queue and move to the actual topic. We need to brush up on such a topic as Queue. So a Queue can be expressed as a linear data structure that follows a particular order in which the operations are performed. We will be learning about Linked Transfer Queue and its different types of implementation.
Linked Transfer Queue
Java's Linked Transfer Queue class is part of the Java Collection frameworks introduced in JDK 1.7 and belongs to Java.util.concurrent package. These elements are in the Linked transfer Queue, which provides an unbounded functionality based on linked nodes. The details in the Linked Transfer Queue are in the FIFO order as the head points to the element that has been in the Queue for the longest time, whereas the tail points to the element that has been for the shortest time.
Syntax
public class LinkedTransferQueue<E> extends AbstractQueue<E> implements TransferQueue<E>, Serialiazable
LinkedTransferQueue(): This constructor will create an empty new queue.
Linked TransferQueue<E> LTQ = new Linked TransferQueue<E>();
Example 1
import java.util.concurrent.LinkedTransferQueue;
import java.util.*;
public class LinkedTransfer {
public static void main(String[] args)throws InterruptedException
{
LinkedTransferQueue<Integer> LTQ = new LinkedTransferQueue<Integer>();
LTQ.add(10);
LTQ.add(20);
LTQ.add(30);
LTQ.add(40);
System.out.println("Linked Transfer Queue1: " + LTQ);
LinkedTransferQueue<Integer> LTQ2 = new LinkedTransferQueue<Integer>(LTQ);
System.out.println("Linked Transfer Queue2: " + LTQ2);
}
}
Output
Linked Transfer Queue1: [10, 20, 30, 40]
Linked Transfer Queue2: [10, 20, 30, 40]
Method 2
LinkedTransferQueue (Collection<E> e): This constructor will construct a queue with the collection elements passed as the parameter.
LinkedTransferQueue<E> LTQ = new LinkedTransferQueue<E>(Collection<E> c);
Example 2
import java.util.concurrent.LinkedTransferQueue;
import java.util.*;
public class LinkedTransferAdd {
public static void main(String[] args)
throws InterruptedException
{
LinkedTransferQueue<Integer> LTQ = new LinkedTransferQueue<Integer>();
LTQ.add(1);
LTQ.add(2);
LTQ.add(3);
LTQ.add(4);
System.out.println("Linked Transfer Queue: " + LTQ);
System.out.println("Size of Linked Transfer Queue: " + LTQ.size());
System.out.println("First element: " + LTQ.poll());
System.out.println("Linked Transfer Queue: " + LTQ);
System.out.println("Size of Linked Transfer Queue: " + LTQ.size());
LTQ.offer(20);
System.out.println("Linked Transfer Queue: " + LTQ);
System.out.println("Size of Linked Transfer Queue: " + LTQ.size());
}
}
Output
Linked Transfer Queue: [1, 2, 3, 4]
Size of Linked Transfer Queue: 4
First element: 1
Linked Transfer Queue: [2, 3, 4]
Size of Linked Transfer Queue: 3
Linked Transfer Queue: [2, 3, 4, 20]
Size of Linked Transfer Queue: 4
In this example, we have used several inbuilt functions of the Linked Transfer Queue such as size, the first element, and so on. We also have shown the output of these functions. Also try it on java online compiler.
Methods of Linked Transfer Queue
Basic Operations
Adding Elements
This method, add(E e), allows us to add the elements in the Linked Transfer Queue.
Code
import java.util.concurrent.*;
public class AddingElements {
public static void main(String[] args) {
LinkedTransferQueue<Integer> queue = new LinkedTransferQueue<Integer>();
for (int i = 10; i <= 14; i++)
queue.add(i);
System.out.println("adding 15 " + queue.offer(15, 5, TimeUnit.SECONDS));
for (int i = 16; i <= 20; i++)
queue.put(i);
System.out.println("The elements in the queue are:");
for (Integer i : queue)
System.out.print(i + " ");
System.out.println();
LinkedTransferQueue<String> g = new LinkedTransferQueue<String>();
new Thread(new Runnable() {
public void run()
{
try {
System.out.println("Transferring" + " an element");
g.transfer("is a computer" + " science portal.");
System.out.println("Element " + "transfer is complete");
}
catch (InterruptedException e1) {
System.out.println(e1);
}
catch (NullPointerException e2) {
System.out.println(e2);
}
}
})
.start();
try {
System.out.println("Coding Ninjas " + g.take());
}
catch (Exception e) {
System.out.println(e);
}
}
}
You can also try this code with Online Java Compiler
adding 15 true
The elements in the queue are:
10 11 12 13 14 15 16 17 18 19 20
Transferring an element
Element transfer is complete
Coding Ninjas is a computer science portal.
You can also try this code with Online Java Compiler
In this example, we have used to add the elements to add them to the Linked transfer Queue, and also we have shown the output.
Removing Elements
Ṭhe remove() method, provided by the Linked Transfer Queue, is used to remove an element supplied if it’s present in the queue.
Code
import java.util.concurrent.LinkedTransferQueue;
public class RemoveElement {
public static void main(String[] args) {
LinkedTransferQueue<Integer> queue = new LinkedTransferQueue<Integer>();
for (int i = 1; i <= 5; i++)
queue.add(i);
System.out.println("The elements in the queue are:");
for (Integer i : queue)
System.out.print(i + " ");
queue.remove(1);
queue.remove(5);
System.out.println("\nRemaining elements in queue : ");
for (Integer i : queue)
System.out.print(i + " ");
}
}
You can also try this code with Online Java Compiler
What is LinkedTransferQueue? A concurrent blocking queue is implemented such that producers may have to wait for the message to be received by the consumer.
When to use Transfer Queue? A Transfer Queue is applicable in messaging applications in which the consumer has to wait for some time to receive the message.
What is Blocking Queue in Java? A queue supports some operation that waits for the Queue to become non-empty when retrieving or removing the elements.
Conclusion
In this article, we have explained Linked Transfer Queue. We have briefly explained the topic and various other topics which might be needed to begin the article. We have also discussed its syntax and several examples and methods with its description. We hope this blog might have helped you enhance your knowledge of Linked Transfer Queue. If you want to learn more about such topics, please visit Introduction to Queue, Problems on Queue. We hope that this blog might have helped you in enhancing your knowledge. If you liked this article, please give it a thumbs up, which might help me and other ninjas grow. "Happy Coding!".