LinkedTransferQueue drainTo() method in Java
Last Updated :
26 Nov, 2018
drainTo(Collection super E> c)
The drainTo(Collection c) method of java.util.concurrent.LinkedTransferQueue Class is an in-built function in Java which removes all the elements present in this queue and adds them to the provided collection. This is a more efficient way than repeatedly polling this queue.
There is also possibilities of failure encountered while attempting to add elements to collection c from the queue and due to that failure, elements is distributed between both collections when the associated exception is thrown. If a queue is tried to drainTo() to queue itself, then IllegalArgumentException will be thrown. If the specified collection is modified while the operation is in progress, the behavior of this operation is undefined. So for using such methods, one needs to take care of this type of situation to overcome exceptions.
Syntax:
public int drainTo(Collection super E> c)
Parameters: The function accepts a mandatory parameter c which is the collection to which elements are to be transferred.
Return Value: The function returns the number of elements drained to collection from queue.
Exceptions: This method throws following exceptions:
- NullPointerException– if the collection is null
- IllegalArgumentException– if arguments of the method prevents it from being added to the specified collection
Below programs illustrate the use of java.util.concurrent.LinkedTransferQueue.drainTo() method:
Program 1: Program to drain all the elements from the queue to the specified collection.
import java.util.*;
import java.util.concurrent.LinkedTransferQueue;
class GFG {
public static void main(String[] args)
{
List<Integer> list = new ArrayList<Integer>();
LinkedTransferQueue<Integer>
queue = new LinkedTransferQueue<Integer>();
for ( int i = 10 ; i <= 15 ; i++)
queue.add(i);
System.out.println( "Elements in the queue = "
+ queue);
queue.drainTo(list);
System.out.println( "Elements left in the queue :"
+ queue);
System.out.println( "Elements drained in the list :"
+ list);
}
}
|
Output:
Elements in the queue = [10, 11, 12, 13, 14, 15]
Elements left in the queue :[]
Elements drained in the list :[10, 11, 12, 13, 14, 15]
Program 2: Program to show the NullPointerException in drainTo().
import java.util.ArrayList;
import java.util.concurrent.LinkedTransferQueue;
class GFG {
public static void main(String[] args)
throws InterruptedException
{
LinkedTransferQueue<Integer>
queue = new LinkedTransferQueue<Integer>();
queue.put( 10 );
queue.put( 20 );
queue.put( 30 );
ArrayList<Integer> add = null ;
try {
queue.drainTo(add);
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
Exception: java.lang.NullPointerException
drainTo(Collection super E> c, int maxElements)
The drainTo(Collection super E> c, int maxElements) method of java.util.concurrent.LinkedTransferQueue is an in-built function in Java which is used to transfer fixed number elements which is passed as integer in drainTo() to collection which is also passed as parameter to method. After transferring the elements, LinkedTransferQueue has only those elements which are not transferred to collection. This function is same as above function with some limitations to transfer fixed no of element.
Syntax:
public int drainTo(Collection super E> c,
int maxElements)
Parameter: The method accepts two parameters:
- c– It represents the collection to transfer elements from LinkedTransferQueue.
- maxElements– This is of integer type and refers to the maximum number of elements to be transferred to the collection.
Return Value: The function returns the number of elements drained to collection from queue.
Exception: This method throws following exceptions:
- NullPointerException – if the collection is null
- IllegalArgumentException – if arguments of the method prevents it from being added to the specified collection
Program 1: Program to drain at most the given number of available elements from the queue to the specified collection.
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedTransferQueue;
class GFG {
public static void main(String[] args)
{
List<Integer> list = new ArrayList<Integer>();
LinkedTransferQueue<Integer>
queue = new LinkedTransferQueue<Integer>();
for ( int i = 1 ; i <= 10 ; i++)
queue.add(i);
System.out.println( "Elements in the queue = "
+ queue);
queue.drainTo(list, 5 );
System.out.println( "Elements left in the queue :"
+ queue);
System.out.println( "Elements drained in the list :"
+ list);
}
}
|
Output:
Elements in the queue = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Elements left in the queue :[6, 7, 8, 9, 10]
Elements drained in the list :[1, 2, 3, 4, 5]
Program 2: Program to show the NullPointerException in drainTo().
import java.util.ArrayList;
import java.util.concurrent.LinkedTransferQueue;
class GFG {
public static void main(String[] args)
throws InterruptedException
{
LinkedTransferQueue<Integer>
queue = new LinkedTransferQueue<Integer>();
queue.put( 10 );
queue.put( 20 );
queue.put( 30 );
ArrayList<Integer> add = null ;
try {
queue.drainTo(add, 2 );
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
Exception: java.lang.NullPointerException
Reference: