Java LinkedTransferQueue drainTo() methodThe 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:Parameterc - 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. ExceptionsNullPointerException - This exception will throw if the specified collection is null. Example 1Test it NowOutput: Elements int the queue = [0, 1] Elements left in the queue :[] Elements drained in the list[0, 1] Example 2Test it NowOutput: 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) methodThe 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:Parameterc - 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. ExceptionsNullPointerException ? This exception will throw if the specified collection is null. Example 3Test 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 4Test it NowOutput: Elements int the queue = [Harry, Jack, Kristan] Elements left in the queue :[Kristan] Elements drained in the list[Harry, Jack] Next TopicJava LinkedTransferQueue |
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