Java ArrayBlockingQueue remove() 方法

2025年1月7日 | 阅读 2 分钟

ArrayBlockingQueue 类的 remove() 方法从队列中移除指定的元素,前提是该元素存在于队列中。

语法

参数

传递的参数 'o' 是要从队列中移除的元素,如果该元素存在的话。

指定者:

ArrayBlockingQueue 类的 remove() 方法由以下接口指定:

  • BlockingQueue<E> 接口中的 remove。
  • Collection<E> 接口中的 remove。

返回值

remove() 方法返回一个布尔值,对应于该对象 'o'。

  • 如果元素存在于队列中,并且队列因此发生了更改,则返回 true。
  • 否则,返回 false。

示例 1

输出

Elements in queue : [12, 98, 122, 102, 112]
Remaining elements in queue : [12, 98, 112]

示例 2

输出

Cake
Pulses
Sugar
Salt
Basmati Rice
Aata
New List : [Pulses, Sugar, Basmati Rice, Aata]