Java Program to Implement LinkedTransferQueue API Last Updated : 27 Apr, 2021 Comments Improve Suggest changes Like Article Like Report LinkedTransferQueue is a queue that orders elements FIFO (first-in-first-out) with respect to any given producer. The head of the queue is that element that has been on the queue the longest time for some producer. The tail of the queue is that element that has been on the queue the shortest time for some producer. Constructor Summary 1. LinkedTransferQueue​(): Creates an initially empty LinkedTransferQueue. 2. LinkedTransferQueue​(Collection<? extends E> c): Creates a LinkedTransferQueue initially containing the elements of the given collection, added in traversal order of the collection's iterator. Java // Java Program to Implement LinkedTransferQueue API import java.util.Collection; import java.util.Iterator; import java.util.concurrent.LinkedTransferQueue; import java.util.concurrent.TimeUnit; public class LinkedTransferQueueImpl<E> { private LinkedTransferQueue<E> linkedTransferQueue; // Create an empty LinkedTransferQueue public LinkedTransferQueueImpl() { linkedTransferQueue = new LinkedTransferQueue<E>(); } // Creates a LinkedTransferQueue containing // the elements of the given collection public LinkedTransferQueueImpl( Collection<? extends E> c) { linkedTransferQueue = new LinkedTransferQueue<E>(c); } // add the given element at the end or tail // of queue public boolean add(E e) { return linkedTransferQueue.add(e); } // remove all the elements from queue public void clear() { linkedTransferQueue.clear(); } // return true if the given object is present public boolean contains(Object o) { return linkedTransferQueue.contains(o); } // Returns an estimate of the number of consumers // waiting to receive elements via BlockingQueue.take() // or timed poll. public int getWaitingConsumerCount() { return linkedTransferQueue .getWaitingConsumerCount(); } // Returns true if there is at least one consumer // waiting to receive an element via // BlockingQueue.take() or timed poll. public boolean hasWaitingConsumer() { return linkedTransferQueue.hasWaitingConsumer(); } // remove all the elements from the queue and add them // to the provided Collection public int drainTo(Collection<? super E> c) { return linkedTransferQueue.drainTo(c); } // Removes at most the given number of available // elements from this queue and adds them to the given // collection. public int drainTo(Collection<? super E> c, int maxElements) { return linkedTransferQueue.drainTo(c, maxElements); } // Returns an iterator over the elements in this queue // in proper sequence. public Iterator<E> iterator() { return linkedTransferQueue.iterator(); } // Inserts the specified element at the tail of this // queue if it is possible to do so immediately without // exceeding the queue's capacity, returning true upon // success and false if this queue is full. public boolean offer(E e) { return linkedTransferQueue.offer(e); } // Inserts the specified element at the tail of this // queue, waiting up to the specified wait time for // space to become available if the queue is full. public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { return linkedTransferQueue.offer(e, timeout, unit); } // Retrieves, but does not remove, the head of this // queue, or returns null if this queue is empty. public E peek() { return linkedTransferQueue.peek(); } // Retrieves and removes the head of this queue, or // returns null if this queue is empty. public E poll() { return linkedTransferQueue.poll(); } // Retrieves and removes the head of this queue, waiting // up to the specified wait time if necessary for an // element to become available. public E poll(long timeout, TimeUnit unit) throws InterruptedException { return linkedTransferQueue.poll(timeout, unit); } // Inserts the specified element at the tail of this // queue, waiting for space to become available if the // queue is full. public void put(E e) throws InterruptedException { linkedTransferQueue.put(e); } // Returns the number of additional elements that this // queue can ideally (in the absence of memory or // resource constraints) accept without blocking. public int remainingCapacity() { return linkedTransferQueue.remainingCapacity(); } // Removes a single instance of the specified element // from this queue, if it is present. public boolean remove(Object o) { return linkedTransferQueue.remove(o); } // Returns the number of elements in this queue. **/ public int size() { return linkedTransferQueue.size(); } // Retrieves and removes the head of this queue, waiting // if necessary until an element becomes available public E take() throws InterruptedException { return linkedTransferQueue.take(); } // Returns an array containing all of the elements in // this queue, in proper sequence. public Object[] toArray() { return linkedTransferQueue.toArray(); } // Returns an array containing all of the elements in // this queue, in proper sequence; the runtime type of // the returned array is that of the specified array. public <T> T[] toArray(T[] a) { return linkedTransferQueue.toArray(a); } // Returns a string representation of this collection. public String toString() { return linkedTransferQueue.toString(); } // Transfers the element to a consumer, waiting if // necessary to do so. public void transfer(E e) throws InterruptedException { linkedTransferQueue.transfer(e); } public static void main(String[] args) { // create a linkedtransfer queue LinkedTransferQueueImpl<String> linkedTransferQueue = new LinkedTransferQueueImpl<String>(); try { // add elements linkedTransferQueue.put("1"); linkedTransferQueue.put("2"); linkedTransferQueue.put("3"); } catch (InterruptedException e) { e.printStackTrace(); } linkedTransferQueue.add("4"); linkedTransferQueue.add("5"); System.out.println( "the elements of the linkedTransferQueue is "); // iterate and print elements Iterator<String> itr = linkedTransferQueue.iterator(); while (itr.hasNext()) { System.out.print(itr.next() + "\t"); } System.out.println(); linkedTransferQueue.offer("6"); linkedTransferQueue.offer("7"); System.out.println( "the peak element of the linkedTransferQueue is(by peeking) " + linkedTransferQueue.peek()); System.out.println( "the peak element of the linkedTransferQueue is(by polling) " + linkedTransferQueue.poll()); System.out.println( "the remaining capacity is " + linkedTransferQueue.remainingCapacity()); System.out.println( "the remaining consumer waiting count : " + linkedTransferQueue .getWaitingConsumerCount()); System.out.println( "element 300 removed " + linkedTransferQueue.remove("3")); System.out.println( "the linkedTransferQueue contains 400 :" + linkedTransferQueue.contains("4")); System.out.println( "the linkedTransferQueue contains 100 :" + linkedTransferQueue.contains("1")); System.out.println( "the size of the linkedTransferQueue is " + linkedTransferQueue.size()); System.out.println(linkedTransferQueue); } } Comment More infoAdvertise with us Next Article Java Program to Implement LinkedTransferQueue API M mayanktyagi1709 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-LinkedTransferQueue +1 More Practice Tags : Java Similar Reads Java Program to Implement LinkedBlockingQueue API LinkedBlockingQueue API is an optionally-bounded queue based on linked nodes. It orders the elements in FIFO(First In First Out) order. The head of this queue is the element that has been there in the queue for the longest time and the tail of the queue is the element that has been in the queue for 6 min read Java Program to Implement LinkedList API Linked List is a part of the Collection framework That is present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure in which the elements are not stored in contiguous locations and every element is a separate object with a data pa 10 min read Java Program to Implement PriorityQueue API A PriorityQueue is a linear data structure in which the elements are ordered according to their natural ordering or by some custom comparator provided at the queue at construction time. In PriorityQueue, the front of the queue points to the least element, and the rear points to the greatest element 4 min read Java Program to Implement SynchronousQueue API SynchronousQueue is a special blocking queue with no internal capacity. It helps in exchange data or information between threads in a thread-safe manner. SynchronousQueue has only 2 supported operations: Both of these are blocking method which means when we want to add a piece of information or data 3 min read Java Program to Implement DelayQueue API The DelayQueue class belongs to java.util.concurrent package. DelayQueue implements the BlockingQueue interface. DelayQueue is a specialized Priority Queue that orders elements supported their delay time. It means that only those elements can be taken from the queue whose time has expired. DelayQueu 7 min read Java Program to Implement ArrayBlockingQueue API ArrayBlockingQueue class is a member of the Java Collection framework. ArrayBlockingQueue is a bounded blocking queue. The term bounded, means that the size of the Queue is fixed and cannot be changed. Any attempt to put element/elements into a full queue will lead to blocking operation. Similarly, 7 min read How to Implement Generic LinkedList in Java? Linked List is Linear Data Structures that store values in nodes. As we do know here each Node possesses two properties namely the value of the node and link to the next node if present so. Linked List can not only be of Integer data type but String, boolean, Float, Character, etc. We can implement 8 min read Java Program to Implement ConcurrentLinkedQueue API The ConcurrentLinkedQueue class in Java is a part of the Java Collection Framework. It belongs to java.util.concurrent package. It was introduced in JDK 1.5. It is used to implement Queue with the help of LinkedList concurrently. It is an unbounded thread-safe implementation of Queue which inserts e 4 min read Java Program to Implement ConcurrentLinkedDeque API ConcurrentLinkedDeque class in Java is an unbounded concurrent deque that stores its elements as linked nodes where each node contains the address of the previous as well as next nodes. It belongs to java.util.concurrent package. This class is a member of the Java Collections Framework. It also exte 3 min read Java Networking Programs - Basic to Advanced Java allows developers to create applications that can communicate over networks, connecting devices and systems together. Whether you're learning about basic connections or diving into more advanced topics like client-server applications, Java provides the tools and libraries you need. This Java Ne 3 min read Like