队列接口的poll()vs remove()方法 (poll() vs remove() methods of Queue Interface )
Here, we will see how poll() method differs from remove() method of Queue interface in Java?
在这里,我们将看到poll()方法与Java中Queue接口的remove()方法有何不同?
poll()方法 (poll() method)
This method is available in java.util package.
此方法在java.util包中可用。
This method is used to retrieve the head element of the Queue or in other words, it is used to retrieve the first element or initial element of the queue.
此方法用于检索队列的head元素,换句话说,它用于检索队列的第一个元素或初始元素。
In the case of poll() method, it retrieves the head element of the queue and then removes the head element of the queue.
对于poll()方法,它将检索队列的head元素,然后删除队列的head元素。
In case of poll() method if the queue is empty then it will return null but it does not throw an exception.
对于poll()方法,如果队列为空,则它将返回null,但不会引发异常。
The syntax of this method is given below:
该方法的语法如下:
public PriorityQueue poll(){ }
We don't pass any object as a parameter in the method of the Queue.
我们不会在Queue方法中将任何对象作为参数传递。
The return type of this method is not void that means this method return first element of the Queue.
该方法的返回类型不是void,这意味着该方法返回Queue的第一个元素。
Example (Case 1):
示例(案例1):
// Java program to demonstrate the behavior of poll() method
// of Q