Java LinkedBlockingDeque drainTo() 方法

2025 年 3 月 21 日 | 阅读 2 分钟

LinkedBlockingDeque() 类的 drainTo() 方法会移除此队列中的所有元素,并将它们添加到提供的集合中。

语法

参数

c - 这是元素被传输到的集合。

maxElements - 这是要传输的最大元素数量。

指定者:

LinkedBlockingDeque 类的 drainTo() 方法是由 BlockingQueue<E> 接口中的 drainTo() 方法指定的。

返回值

drainTo() 方法返回被传输的元素数量。

抛出

drainTo() 方法会抛出

UnsupportedOperationException - 如果指定的集合不支持添加元素。

ClassCastException - 如果此队列中的某个元素的类阻止其被添加到定义的集合中。

NullPointerException - 如果定义的集合为 null。

IllegalArgumentException - 如果定义的集合是此队列本身。

示例 1

输出

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

示例 2

输出

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]

示例 3

输出

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