Java中队列接口的poll()和remove()方法之间的区别

本文探讨了Java中Queue接口的poll()和remove()方法的区别。poll()方法在队列不为空时返回并移除头元素,若队列为空则返回null;而remove()方法同样移除头元素,但如果队列为空会抛出NoSuchElementException异常。
摘要由CSDN通过智能技术生成

队列接口的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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值