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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

队列接口的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 Queue in case of if Queue is not empty

import java.util.*;

class QueueClass {
    public static void main(String[] args) {
        // Creating an instance of PriorityQueue class
        PriorityQueue pq = new PriorityQueue();

        // By using add() method to add elements in the Queue
        pq.add(10);
        pq.add(20);
        pq.add(30);
        pq.add(40);
        pq.add(50);

        // Display Current list of the Queue
        System.out.println("Current Queue List:" + pq);

        // By using poll() method of Queue will retrieve 
        // head element with removing head element of the Queue
        System.out.println("The first element of the Queue :" + pq.poll());

        // Display New Queue list of the Queue after implementing poll() method
        System.out.println("New Queue List :" + pq);
    }
}

Output

输出量

E:\Programs>javac QueueClass.java

E:\Programs>java QueueClass
Current Queue List:[10, 20, 30, 40, 50]
The first element of the Queue :10
New Queue List :[20, 40, 30, 50]

Example (Case 2:)

示例(案例2 :)

// Java program to demonstrate the behavior of poll() method 
// of Queue in case of if Queue is empty

import java.util.*;

class QueueClass {
    public static void main(String[] args) {
        // Creating an instance of PriorityQueue class
        PriorityQueue pq = new PriorityQueue();

        // Display Current list of the Queue
        System.out.println("Current Queue List:" + pq);

        // By using poll() method of Queue will return null if queue is empty
        System.out.println("The result of Queue :" + pq.poll());

        // Display New Queue list of the Queue after implementing poll() method
        System.out.println("New Queue List :" + pq);
    }
}

Output

输出量

E:\Programs>javac QueueClass.java

E:\Programs>java QueueClass
Current Queue List:[]
The first element of the Queue :null
New Queue List :[]

Now, we will see how remove() method differs from poll() method of Queue interface?

现在,我们将看到remove()方法与Queue接口的poll()方法有何不同?

remove()方法 (remove() method)

  • This method is available in java.util package.

    此方法在java.util包中可用。

  • This method is used to remove the head element of the Queue and retrieve the first element of the Queue like poll() method.

    此方法用于删除Queue的head元素,并像poll()方法一样检索Queue的第一个元素。

  • In the case of remove() method, it retrieves the head element and removes the first element of the Queue as well by calling remove() method.

    对于remove()方法,它通过调用remove()方法来检索head元素并删除Queue的第一个元素。

  • In case of remove() method, if Queue is empty then, in that case, it throws an exception NoSuchElementFoundException but it does not return null like of poll() method.

    在remove()方法的情况下,如果Queue为空,则在这种情况下,它将引发NoSuchElementFoundException异常,但不会像poll()方法那样返回null。

  • We don't pass any object as a parameter in the method of the Queue.

    我们不会在Queue方法中将任何对象作为参数传递。

  • The syntax of the method is given below:

    该方法的语法如下:

        public boolean remove(){}
    
    
  • The return type of this method is not void that means the return type of this method is boolean so it returns true after removing the element else return false.

    此方法的返回类型不是无效的,这意味着此方法的返回类型为布尔值,因此在删除元素后它返回true,否则返回false。

Example (Case 1):

示例(案例1):

// Java program to demonstrate the behavior of remove() method 
// of Queue in case of if Queue is not empty

import java.util.*;

class QueueClass {
    public static void main(String[] args) {
        // Creating an instance of PriorityQueue class
        PriorityQueue pq = new PriorityQueue();

        // By using add() method to add elements in the Queue
        pq.add(10);
        pq.add(20);
        pq.add(30);
        pq.add(40);
        pq.add(50);

        // Display Current list of the Queue
        System.out.println("Current Queue List:" + pq);

        // By using remove() method of Queue will retrieve 
        // head element with removing head element of the Queue
        System.out.println("The first element of the Queue :" + pq.remove());

        // Display New Queue list of the Queue after 
        // implementing remove() method
        System.out.println("New Queue List :" + pq);
    }
}

Output

输出量

E:\Programs>javac QueueClass.java

E:\Programs>java QueueClass
Current Queue List:[10, 20, 30, 40, 50]
The first element of the Queue :10
New Queue List :[20, 40, 30, 50]

Example (Case 2):

示例(案例2):

// Java program to demonstrate the behavior of remove() method 
// of Queue in case of if Queue is empty

import java.util.*;

class QueueClass {
    public static void main(String[] args) {
        // Creating an instance of PriorityQueue class
        PriorityQueue pq = new PriorityQueue();

        // Display Current list of the Queue
        System.out.println("Current Queue List:" + pq);

        // By using remove() method of Queue will throw 
        // an exception if queue is empty
        System.out.println("The result of Queue :" + pq.remove());

        // Display New Queue list of the Queue after 
        // implementing remove() method
        System.out.println("New Queue List :" + pq);
    }
}

Output

输出量

E:\Programs>javac QueueClass.java

E:\Programs>java QueueClass
Current Queue List:[]
Exception in thread "main" java.util.NoSuchElementException
        at java.util.AbstractQueue.remove(AbstractQueue.java:117)
        at QueueClass.main(QueueClass.java:20)


翻译自: https://www.includehelp.com/java/differences-between-poll-and-remove-methods-of-queue-interface-in-java.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值