Java PriorityQueue remove()方法与示例

PriorityQueue类remove()方法 (PriorityQueue Class remove() method)

  • remove() method is available in java.util package.

    remove()方法在java.util包中可用。

  • remove() method is used to remove one instance of the given object from this PriorityQueue when exists.

    remove()方法用于从该PriorityQueue中删除给定对象的一个​​实例(如果存在)。

  • remove() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    remove()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • remove() method does not throw an exception at the time of removing an object.

    remove()方法在删除对象时不会引发异常。

Syntax:

句法:

    public boolean remove(Object ob);

Parameter(s):

参数:

  • Object ob – represents the element (ele) to be remove from this PriorityQueue.

    对象ob –表示要从此PriorityQueue中删除的元素(ele)。

Return value:

返回值:

The return type of the method is boolean, it returns true when the given element is to be removed successfully otherwise it returns false.

方法的返回类型为boolean ,如果要成功删除给定元素,则返回true,否则返回false。

Example:

例:

// Java program to demonstrate the example 
// of boolean remove(Object ob) method of 
// PriorityQueue

import java.util.*;

public class RemoveOfPriorityQueue {
    public static void main(String args[]) {
        // Instantiate PriorityQueue
        PriorityQueue < String > pq = new PriorityQueue < String > ();

        // By using add() method is add
        // the given element into priority
        // queue
        pq.add("C");
        pq.add("C++");
        pq.add("JAVA");
        pq.add("PHP");
        pq.add("ANDROID");
        pq.add("JAVA");

        // Display PriorityQueue
        System.out.println("PriorityQueue: " + pq);

        // By using remove(object) method is to
        // remove the single instance of given element
        // from this PriorityQueue
        Object ele = pq.remove("JAVA");

        // Display removed element
        System.out.println("pq.remove(JAVA): " + ele);

        // Display PriorityQueue
        System.out.println("PriorityQueue: " + pq);
    }
}

Output

输出量

PriorityQueue: [ANDROID, C, JAVA, PHP, C++, JAVA]
pq.remove(JAVA): true
PriorityQueue: [ANDROID, C, JAVA, PHP, C++]


翻译自: https://www.includehelp.com/java/priorityqueue-remove-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PriorityQueueJava中的一种实现了优先队列的数据结构。它是一个队列,具有能够优先处理队列中元素的特性。PriorityQueue使用堆来实现其内部容器,保证最小的元素总是在队首,可以用来实现一些贪心算法和Dijkstra最短路径算法等。 PriorityQueue有两个主要的构造函数: 1. PriorityQueue():创建一个空的PriorityQueue。 2. PriorityQueue(Collection c):使用集合c元素初始化PriorityQueue,支持所有Collection的操作。 PriorityQueue有以下常用方法: 1. add(E e):将元素e插入到PriorityQueue中。 2. offer(E e):将元素e插入到PriorityQueue中。 3. peek():获取队列头部的元素,但不移除队列中的元素。 4. poll():获取并移除队列头部的元素。 5. remove(Object o):从队列中移除元素o。 6. contains(Object o):判断队列中是否包含元素o。 7. size():返回队列的大小。 示例: ``` PriorityQueue<Integer> pq = new PriorityQueue<>(); pq.add(1); pq.add(2); pq.add(3); System.out.println(pq.peek()); //输出1 pq.poll(); System.out.println(pq.peek()); //输出2 System.out.println(pq.contains(3)); //输出true ``` 在上面的代码中,我们创建了一个PriorityQueue,并使用add()方法将三个元素1,2,3插入到队列中。然后我们使用peek()方法获取队列头部的元素并输出结果为1,然后我们使用poll()方法获取并移除队列头部的元素,并再次使用peek()方法获取队列头部的元素,并输出结果为2。最后我们使用contains()方法检查队列中是否包含元素3,并输出结果为true。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值