1. 程式人生 > >Java Queue用法總結

Java Queue用法總結

佇列,一種結構體,遵循先進先出的原則。

java中提供的方法有以下幾種:


操作有插入、刪除和檢查某個下標是否有值三種,其中每種操作包含兩個函式,它們的功能是相同的,區別在於insert,remove,element三個函式在操作失敗是報異常,而offer,poll,peek三個函式不報異常。

具體描述原句是這樣的:

Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false

, depending on the operation). 

但是這種說法並不絕對,poll和peek確實不報異常,但是offer函式在某些情況下會報異常,具體如下:

 - if the class of the specified element prevents it from being added to this queue - if the specified element is null and this queue does not permit null elements

 - if some property of this element prevents it from being added to this queue

但是插入發生異常的情況可能性比較小。

在以後的使用中,要根據應用場景選擇合適的函式。