java Queue 的remove/poll, add/offer, element/peek

Original link: https://blog.csdn.net/liuyongvs2009/article/details/42454779

Here it is simple to do simple distinction to its repeatable method.

offer, add the difference:

Some queue is limited in size, so if you want to add a new item in a queue is full, the extra items will be rejected.

At this new offer methods can play a role. It is not a call to add () method throws an unchecked exception, just get false by the offer () returned. 

 

poll, remove differences:

remove () and poll () method is removing the first element from the queue. remove () version of the Collection interface similar behavior,

But the new poll () method when invoked with an empty collection instead of throwing an exception, just return null. So the new method is more suitable for abnormal conditions are likely to occur.

 

peek, element difference:

element () and peek () to query elements in the head of the queue. And remove () like, when the queue is empty, Element () throws an exception, the peek () returns null

Guess you like

Origin www.cnblogs.com/doyi111/p/11577402.html