队列的add()方法和offer()方法的区别 poll,peek,element的区别

add():Inserts the specified element at the tail of this queue. As the queue is unbounded, this method will never throw IllegalStateException or return false.

offer():Inserts the specified element at the tail of this queue. As the queue is unbounded, this method will never return false.

 区别:两者都是往队列尾部插入元素,不同的时候,当超出队列界限的时候,add()方法是抛出异常让你处理,而offer()方法是直接返回false

 

poll,peek,element的共同点:
都是返回队列中的首个元素
不同点:

poll:将首个元素从队列中弹出,如果队列是空的,就返回null
peek:查看首个元素,不会移除首个元素,如果队列是空的就返回null
element:查看首个元素,不会移除首个元素,如果队列是空的就抛出异常NoSuchElementException

posted @   Leo的银弹  阅读(396)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示