队列的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 @ 2022-01-20 22:02  Leo的银弹  阅读(362)  评论(0编辑  收藏  举报