news 2026/7/4 14:24:44

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/7/4 10:43:18

50、网络故障排除与监控实用指南

网络故障排除与监控实用指南 1. 使用 ngrep 进行高级数据包嗅探 1.1 基本使用 ngrep 是一个强大的数据包嗅探工具,可帮助我们进行网络故障排查。例如,要在 eth0 接口上查找包含 1234 的 ICMP 数据包,可使用以下命令: # ngrep -qpd eth0 1234 icmp输出示例如下: …

作者头像 李华
网站建设 2026/7/4 3:31:19

7、雾无线接入网络中的协作信号处理技术解析

雾无线接入网络中的协作信号处理技术解析 1. F-RANs理论性能与边缘缓存 在F-RANs(雾无线接入网络)中,边缘缓存和信号处理对系统性能有着重要影响。相关参数设置为:(\varphi_{C}^{k} = 0.15),(\varphi_{K}^{k} = 0.35),每个内容对象的大小(B_{K} = 10)Mbits。 通过仿真…

作者头像 李华
网站建设 2026/7/3 19:03:55

9、雾无线接入网络中的灵活网络管理

雾无线接入网络中的灵活网络管理 在雾无线接入网络(F-RAN)中,灵活的网络管理至关重要。通过整合来自上层的请求服务和订阅信息、切片实例层的RAN与UE信息以及集中编排层的切片实例配置信息,可以满足切片实例的性能要求并提升整个F-RAN的性能。下面将详细介绍F-RAN接入切片…

作者头像 李华
网站建设 2026/7/3 17:56:28

【打靶日记】HackMyVm 之 icarus

主机发现 ┌──(root㉿xhh)-[~/Desktop/xhh/HMV/icarus] └─# arp-scan -I eth1 -l192.168.56.146 08:00:27:d5:6a:34 PCS Systemtechnik GmbH主机地址为:192.168.56.146 端口扫描 ┌──(root㉿xhh)-[~/Desktop/xhh/HMV/icarus] └─# nmap -p- 192.168.5…

作者头像 李华
网站建设 2026/7/3 10:40:37

9 个降AI率工具,本科生论文必备!

9 个降AI率工具,本科生论文必备! 论文被AI检测“亮红灯”,本科生的焦虑谁懂? 对于许多本科生来说,写论文从来都不是一件轻松的事。从选题到开题,从查资料到写大纲,每一个环节都充满了挑战。而最…

作者头像 李华
网站建设 2026/7/3 4:17:22

9 个降AI率工具,本科生开题报告AI助手推荐

9 个降AI率工具,本科生开题报告AI助手推荐 论文写作的“重灾区”:降重难题让本科生苦不堪言 对于大多数本科生来说,撰写开题报告是大学阶段最令人头疼的任务之一。它不仅需要大量的文献查阅和资料整理,更要在规定字数内清晰地表达…

作者头像 李华