The
drainTo(Collection col) method of BlockingQueue removes all available elements from this LinkedBlocking Queue and adds them to the given collection passed as a parameter.
Note: The
drainTo() method of
BlockingQueue has been inherited from the
Queue class in Java.
drainTo(Collection<? super E> col)
The
drainTo(Collection<? super E> col) method of
BlockingQueue interface removes all of the elements from this queue and adds them to the given collection col. 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> col)
Parameter: This method accepts one parameter
col which represents the collection to transfer elements from LinkedBlockingQueue.
Return Value: This method returns the number of elements drained to collection from queue.
Exception: This method throws following exceptions:
- UnsupportedOperationException- if collection cannot able to add elements.
- ClassCastException- if class of element stops method to add element to collection.
- NullPointerException- if the collection is null
- IllegalArgumentException- if arguments of the method prevents it from being added to the specified collection
Below programs illustrates drainTo() method of BlockingQueue class:
Program 1:
Output:
Before drainTo():
LinkedBlockingQueue :
[Employee [name=Aman, position=Analyst, salary=24000], Employee [name=Sachin, position=Developer, salary=39000]]
ArrayList :
[]
No of element passed: 2
After drainTo():
LinkedBlockingQueue :
[]
ArrayList :
[Employee [name=Aman, position=Analyst, salary=24000], Employee [name=Sachin, position=Developer, salary=39000]]
Program 2:
Output:
Exception: java.lang.NullPointerException
drainTo(Collection<? super E> col, int maxElements)
The
drainTo(Collection<? super E> col, int maxElements) 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, BlockingQueue 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> col, int maxElements)
Parameter: The method accepts two parameters:
- col- It represents the collection to transfer elements from BlockingQueue.
- maxElements- This is of integer type and refers to the maximum number of elements to be transferred to the collection.
Return Value: The method returns the
number of elements drained to collection from queue.
Exception: This method throws following exceptions:
- UnsupportedOperationException - if collection cannot able to add elements.
- ClassCastException -if class of element stops method to add element to collection.
- NullPointerException - if the collection is null
- IllegalArgumentException - if arguments of the method prevents it from being added to the specified collection
Below programs illustrates drainTo(Collection<? super E> col, int maxElements) method of BlockingQueue class
Program 1:
Below program has a BlockingQueue which stores Employee objects and there is a HashSet in which will store all employee objects from BlockingQueue. So drainTo() of BlockingQueue is used to pass some employee from queue to ArrayList. So no of element to be transferred is passed in method as parameter.