news 2026/5/1 9:12:59

Queue(队列)两组增删查操作的区别

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Queue(队列)两组增删查操作的区别

方法表

方法功能
boolean offer(E e)入队列
boolean add(E e)入队列
E poll()出队列
E remove()出队列
E peek()获取队头元素
E element()获取队头元素

Queue有两组增删查的方法,这两组方法实现的效果是一样的,那么他们的区别再哪呢?我们来查看一下。

入队列boolean offer(E e)和boolean add(E e)

boolean offer(E e)

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add, which can fail to insert an element only by throwing an exception.
Params:
e – the element to add
Returns:
true if the element was added to this queue, else false
Throws:
ClassCastException – if the class of the specified element prevents it from being added to this queue
NullPointerException – if the specified element is null and this queue does not permit null elements
IllegalArgumentException – if some property of this element prevents it from being added to this queue

翻译:

尝试立即将指定元素插入此队列,前提是该操作不会违反容量限制。​ 在使用容量受限的队列时,此方法通常优于 add方法,因为 add方法在无法插入元素时只能通过抛出异常来表示失败。
参数:​
e – 要添加的元素
返回:​
如果元素被成功添加到此队列,则返回 true;否则返回 false
抛出:​
ClassCastException – 如果指定元素的类阻止其被添加到此队列
NullPointerException – 如果指定元素为 null 且此队列不允许 null 元素
IllegalArgumentException – 如果此元素的某些属性阻止其被添加到此队列

boolean add(E e)

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
Params:
e – the element to add
Returns:
true (as specified by Collection.add)
Throws:
IllegalStateException – if the element cannot be added at this time due to capacity restrictions
ClassCastException – if the class of the specified element prevents it from being added to this queue
NullPointerException – if the specified element is null and this queue does not permit null elements
IllegalArgumentException – if some property of this element prevents it from being added to this queue

翻译:

如果可以在不违反容量限制的情况下立即将指定元素插入此队列,则插入该元素,成功时返回 true,如果当前没有可用空间,则抛出 IllegalStateException 异常。
参数:
e - 要添加的元素
返回:
true(按照 Collection.add 的规定)
抛出:
IllegalStateException - 如果由于容量限制此时无法添加该元素
ClassCastException - 如果指定元素的类阻止其被添加到此队列
NullPointerException - 如果指定元素为 null 且此队列不允许 null 元素
IllegalArgumentException - 如果此元素的某些属性阻止其被添加到此队列

区别总结

  1. boolean offer(E e)是一种条件性插入操作:仅在队列当前未满时成功,否则立即静默失败(返回 false)。
  2. 与 boolean add(E e)不同,boolean offer(E e)在队列已满时不会抛出异常,因此更适用于需避免异常处理的流量控制或实时场景。

出队列E poll()和E remove()

E poll()

Retrieves and removes the head of this queue, or returns null if this queue is empty.
Returns:
the head of this queue, or null if this queue is empty

翻译:

检索并移除此队列的头元素,如果此队列为空,则返回 null。
返回:​
此队列的头元素,如果此队列为空,则返回 null

E remove()

Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty.
Returns:
the head of this queue
Throws:
NoSuchElementException – if this queue is empty

翻译:

检索并移除此队列的头元素。​ 此方法与 poll的不同之处在于,如果此队列为空,它将抛出异常。
返回:​ 此队列的头元素
抛出:​ NoSuchElementException - 如果此队列为空

区别总结

  1. poll()方法与 remove()方法的核心区别在于:当队列为时,poll()会安全地返回 null,而 remove()会抛出异常
  2. poll()更适用于不确定队列是否为空,且希望避免异常的场景,是一种更稳妥的获取并移除队列头元素的方式。

获取队头元素E peek()和E element()

E peek()

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
Returns:
the head of this queue, or null if this queue is empty

翻译:

检索但不移除此队列的头元素,如果此队列为空,则返回 null。
返回:​
此队列的头元素,如果此队列为空,则返回 null

E element()

Retrieves, but does not remove, the head of this queue. This method differs from peek only in that it throws an exception if this queue is empty.
Returns:
the head of this queue
Throws:
NoSuchElementException – if this queue is empty

翻译:

检索但不移除此队列的头元素。此方法与 peek的唯一区别在于,如果此队列为空,它将抛出异常。
返回:​
此队列的头元素
抛出:​
NoSuchElementException - 如果此队列为空

区别总结

当队列为空时,peek()会安全地返回 null,而 element()会抛出 NoSuchElementException异常。因此,在不确定队列是否为空时,使用 peek()更安全;若确定队列不为空,可使用 element()。

总结

根据对队列空或满时的处理方案,可以将Queue的增删查方法分为两组。

第一组(队列满或空时不抛出异常):
1.boolean offer(E e),队列满时,返回false
2.E poll(),队列为空时,返回null
3.E peek(),队列为空时,返回null

第二组(队列为满或空时会抛出异常)
1.boolean add(E e),队列满时,抛出IllegalStateException异常
2.E remove(),队列为空时,抛出NoSuchElementException异常
3.E element(),队列为空时,抛出NoSuchElementException异常

因此,在对队列进行增删查时,希望避免异常处理时,就调用第一组的方法;希望队列为空或满时抛出异常,就调用第二组的方法。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 23:28:19

49、网络故障排除工具全解析

网络故障排除工具全解析 1. 综合网络诊断工具 mtr mtr 是一款结合了 ping 和 traceroute 功能的实用工具,可用于捕获综合延迟、数据包丢失和问题路由器统计信息。以下是使用 mtr 的详细介绍: 1.1 基本使用 运行 mtr 100 次,将数据整理成报告格式,并存储在文本文件中: …

作者头像 李华
网站建设 2026/5/1 9:07:39

10 个自考论文降重网站,AI 工具推荐助你轻松应对

10 个自考论文降重网站,AI 工具推荐助你轻松应对 论文路上的“重”与“痛”,你是否也经历过? 对于自考学子来说,论文写作从来不是一件轻松的事。从选题、收集资料、撰写初稿,到反复修改、降重、提交,每一个…

作者头像 李华
网站建设 2026/5/1 9:07:26

Day 32模块和库的导入

常见的第三方库: 导入库的三种手段: 1.导入整个库 # 方式1:导入整个模块 import mathprint("方式1:使用 import math") print(f"圆周率π的值:{math.pi}") print(f"2的平方根:{…

作者头像 李华
网站建设 2026/4/16 12:41:19

2、无线接入网与雾计算技术解析

无线接入网与雾计算技术解析 1. 无线接入网的发展与演进 1.1 C - RAN 概述 在传统蜂窝系统中,基站消耗了大部分能量。为优化有限的无线电资源并节省能耗,将存储和计算迁移到“云”中以创建“计算实体”的想法应运而生。C - RAN(集中式无线接入网)作为一种先进的网络架构…

作者头像 李华
网站建设 2026/4/25 11:06:49

3、雾无线接入网络(F-RAN):5G及未来的网络新趋势

雾无线接入网络(F-RAN):5G及未来的网络新趋势 1. 雾计算与边缘计算 雾计算和边缘计算在很多文献中常被互换使用,它们功能相似。OpenFog联盟将雾计算定义为“一种系统级的水平架构,可在从云到物的连续体中的任何位置分配计算、存储、控制和网络的资源与服务”。而工业互联…

作者头像 李华
网站建设 2026/5/1 8:20:39

MATLAB从零开始实现快速傅里叶变换FFT

文章目录 一、基础目标 二、FFT的核心思想 三、实现步骤与 MATLAB 代码 四、重要注意事项与局限性 五、总结 一、基础目标 在 MATLAB 中从零开始实现快速傅里叶变换(FFT)是一项非常有益的工作,有助于深入理解这个核心算法的精妙之处。 二、FFT的核心思想 FFT 并非一种新的…

作者头像 李华