Java LinkedBlockingDeque drainTo() MethodThe 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 1Test it NowOutput: Elements int the queue = [0, 1, 2] Elements left in the queue :[] Elements drained in the list[0, 1, 2] Example 2Test it NowOutput: 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 3Test it NowOutput: Elements int the queue = [Ram, Sham, Sunita] Elements left in the queue :[Sunita] Elements drained in the list[Ram, Sham] Next TopicJava Linkedblockingdeque |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India