关于java中的队列几个重要函数

本文详细介绍了 Java 中 Queue 接口的 offer 方法及其在 LinkedList 类中的实现。探讨了该方法如何在不违反容量限制的情况下尝试立即插入指定元素,并讨论了与 add 方法的区别。此外,还解释了相关方法如 peek 和 poll 的作用。
http://docs.oracle.com/javase/7/docs/api/java/util/Queue.html#offer(E)
offer   来自Queue类
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(E), which can fail to insert an element only by throwing an exception.

Parameters:
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

LinkedList类中的offer方法

public boolean offer(E e)

Adds the specified element as the tail (last element) of this list.

Specified by:
offer in interface Deque<E>
Specified by:
offer in interface Queue<E>
Parameters:
e - the element to add
Returns:
true (as specified by Queue.offer(E))
Since:
1.5

add方法  注意参数e是否可以是null

public boolean add(E e)

Appends the specified element to the end of this list.

This method is equivalent to addLast(E)

 

public E peek()

Retrieves, but does not remove, the head (first element) of this list.

poll
public E poll()

Retrieves and removes the head (first element) of this list.

转载于:https://my.oschina.net/ray1421/blog/790356

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值