Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Linked Transfer Queue
2.1.
Syntax
3.
Hierarchy
4.
Constructors of Linked Transfer Queue
4.1.
Method 1
4.2.
Example 1
4.3.
Method 2
4.4.
Example 2
5.
Methods of Linked Transfer Queue
6.
Basic Operations
6.1.
Adding Elements
6.2.
Code
6.3.
Removing Elements
6.3.1.
Code
7.
FAQs
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Linked Transfer Queue

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

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
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Hierarchy

Also see,  Swap Function in Java

Constructors of Linked Transfer Queue

Method 1

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);
    }
}
}

Output

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.

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 + " ");
 }
}

Output

The elements in the queue are:
1 2 3 4 5 
Remaining elements in queue : 
2 3 4 


In this example, we have added the elements in the queue, removed them, and shown the output.

Also read, Duck Number in Java and Hashcode Method in Java

FAQs

  1. 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.
     
  2. 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.
     
  3. 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 QueueProblems 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!".

Previous article
Linked Blocking Queue
Next article
What is a Circular Queue
Live masterclass