Java LinkedBlockingDeque drainTo() Method

The drainTo() method of LinkedBlockingDeque() class removes all the elements present in this queue and adds them to the provided collection.

Syntax:

Parameters:

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

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

Specified By:

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

Return Value:

The drainTo() method returns the number of elements that are transferred.

Throws:

The drainTo() method throws:

UnsupportedOperationException - if the specified collection doesn't supports the addition of elements.

ClassCastException - if the class of an element in this queue stops it from being added to the defined collection.

NullPointerException - if the defined collection is null.

IllegalArgumentException - if the defined collection is this queue.

Example 1

Test it Now

Output:

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

Example 2

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 3

Test it Now

Output:

Elements int the queue = [Ram, Sham, Sunita]
Elements left in the queue :[Sunita]
Elements drained in the list[Ram, Sham]





Latest Courses