Javatpoint Logo
Javatpoint Logo

Java LinkedTransferQueue drainTo() method

The drainTo(Collection<? super E> c) method of LinkedTransferQueue() class removes all the elements present in this queue and adds them to the provided collection.

Syntax:

Parameter

c - It is the collection to which elements are transferred.

Specified By:

The drainTo() method of LinkedTransferQueue class is specified by drainTo() method in interface BlockingQueue.

Return:

This method returns the number of elements transferred.

Exceptions

NullPointerException - This exception will throw if the specified collection is null.

Example 1

Test it Now

Output:

Elements int the queue = [0, 1]
Elements left in the queue :[]
Elements drained in the list[0, 1]

Example 2

Test it Now

Output:

Elements int the queue = [0, 1, 2]
Elements left in the queue :[]
Elements drained in the list[0, 1, 2]

Java LinkedTransferQueue drainTo(Collection<? super E> c,int maxElements) method

The drainTo(Collection<? super E> c, int maxElements) method of LinkedTransferQueue() class removes all the elements present in this queue and adds them to the provided collection.

Syntax:

Parameter

c - It is the collection to which elements are transferred.

maxElements - It is the maximum number of elements to be transferred.

Specified By:

The drainTo() method of LinkedTransferQueue class is specified by drainTo() method in interface BlockingQueue<E>.

Return:

This method returns the number of elements transferred.

Exceptions

NullPointerException ? This exception will throw if the specified collection is null.

Example 3

Test it Now

Output:

Elements int the queue = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Elements left in the queue :[5, 6, 7, 8, 9, 10]
Elements drained in the list[0, 1, 2, 3, 4]

Example 4

Test it Now

Output:

Elements int the queue = [Harry, Jack, Kristan]
Elements left in the queue :[Kristan]
Elements drained in the list[Harry, Jack]





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials