The queue is a typical fifo (FIFO) The container of . That is to put things from one end of the container , Take out… From the other end , And the order in which things are put into the container is the same as the order in which they are taken out .
Two ways to implement queues :
1、offer() and add() The difference between
add() and offer() It’s all about adding an element to the queue . But if you want to add a new element to a full queue , call add() Method will throw a unchecked abnormal , And call offer() Method will return false. According to this, we can make an effective judgment in the program !
2、peek() and element() The difference between
peek() and element() Will be in Do not remove Back to the team leader , however peek() Method returns… When the queue is empty null, call element() Method will throw NoSuchElementException abnormal .
3、poll() and remove() The difference between
poll() and remove() All will remove And back to the other side , But in poll() Returns… When the queue is empty null, and remove() Will throw out NoSuchElementException abnormal .
The author concludes here , I hope that’s helpful !
Thank you. , We grew up together !