📜  Java中的 PriorityQueue remove() 方法

📅  最后修改于: 2022-05-13 01:55:24.575000             🧑  作者: Mango

Java中的 PriorityQueue remove() 方法

Java.util 包PriorityQueue 类remove() 方法用于从 PriorityQueue 中删除特定元素。众所周知,进入优先级队列的元素没有排序,但众所周知,在从优先级队列中取出元素时,元素始终是排序的,这是优先级队列的一个特征。这里数据类型元素的默认优先顺序定义如下:

  • Integer :首先出现的最小元素(仅处理正数时)
  • 字符串:按字母顺序排列

句法:

PriorityQueue = new PriorityQueue(ComparatorHere);

语法:删除方法

Priority_Queue.remove(Object O)

参数:参数O是PriorityQueue的类型,指定要从PriorityQueue中移除的元素。

返回值:如果指定元素存在于队列中,则此方法返回 True,否则返回 False。

示例 1

Java
// Java Program to Illustrate remove() Method
// in PriorityQueue
// Where Elements are of String Type
 
// Importing all utility classes
import java.util.*;
 
// Main class
// PriorityQueueDemo
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating an empty PriorityQueue
        // where elements are of string type
        PriorityQueue queue
            = new PriorityQueue();
 
        // Adding elements into the Queue
        // using add() method
        queue.add("Welcome");
        queue.add("To");
        queue.add("Geeks");
        queue.add("For");
        queue.add("Geeks");
 
        // Printing the elements of PriorityQueue
        System.out.println("Initial PriorityQueue: "
                           + queue);
 
        // Removing elements from PriorityQueue
        // using remove() method
        queue.remove("Geeks");
        queue.remove("For");
        queue.remove("Welcome");
 
        // Displaying the PriorityQueue
        // after removal of element
        System.out.println("PriorityQueue after removing "
                           + "elements: " + queue);
    }
}


Java
// Java Program to Illustrate remove() Method
// of PriorityQueue class
// Where Elements are of Integer type
 
// Importing required classes
import java.util.*;
 
// Main class
// PriorityQueueDemo
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating an empty PriorityQueue by
        // creating an object of integer type
        PriorityQueue queue
            = new PriorityQueue();
 
        // Adding custom input elements
        // using add() method
        queue.add(10);
        queue.add(15);
        queue.add(30);
        queue.add(20);
        queue.add(5);
 
        // Displaying the PriorityQueue
        System.out.println("Initial PriorityQueue: "
                           + queue);
 
        // Removing elements from the PriorityQueue
        // using remove() method
        queue.remove(30);
        queue.remove(5);
 
        // Displaying the PriorityQueue elements
        // after removal
        System.out.println("PriorityQueue after removing "
                           + "elements: " + queue);
    }
}


Java
// Java Program to illustrate remove() Method
// in PriorityQueue
// Where Exception is encountered
 
// Importing required classes
import java.io.*;
import java.util.PriorityQueue;
 
// Main class
// PriorityQueueException
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an empty PriorityQueue
        PriorityQueue pq
            = new PriorityQueue();
 
        // Note: Elements are inserted in unsorted order in
        // priority queue but after removal of elements
        // queue is always sorted.
 
        // Adding elements in above queue
        // using add() method
        pq.add(2);
        pq.add(14);
        pq.add(41);
        pq.add(7);
        pq.add(99);
 
        // Elements in queue are unsorted by far
 
        // Getting size of above queue before deletion
        // of any element using size() method
        System.out.println(
            "Size of priority queue before deletion : "
            + pq.size());
 
        // Printing all elements of above queue
        System.out.println(
            "Priority queue before removal : " + pq);
 
        // Calling remove() method over priority queue
        // in which there were 5 elements
 
        // Here calling remove() method say be it 2 times
        // So 2 top priority elements will be removed
        System.out.println(" 1st element removed : "
                           + pq.remove());
        System.out.println(" 2nd element removed : "
                           + pq.remove());
        System.out.println(" 3rd element removed : "
                           + pq.remove());
        System.out.println(" 4th element removed : "
                           + pq.remove());
        System.out.println(" 5th element removed : "
                           + pq.remove());
 
        // By now queue is empty and if now we made further
        // remove() call it will throw exception for this
        System.out.println(" 6th element removed : "
                           + pq.remove());
 
        // As we know smaller the integer bigger the
        // priority been set by default comparator of this
        // class
 
        // Note: Now the element is always returned sorted
        // from a priority queue is a trait of this class
 
        // Printing the queue after removal of priority
        // elements
        System.out.println(
            "Priority queue after removal as follows: "
            + pq);
    }
}


输出
Initial PriorityQueue: [For, Geeks, To, Welcome, Geeks]
PriorityQueue after removing elements: [Geeks, To]

示例 2

Java

// Java Program to Illustrate remove() Method
// of PriorityQueue class
// Where Elements are of Integer type
 
// Importing required classes
import java.util.*;
 
// Main class
// PriorityQueueDemo
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating an empty PriorityQueue by
        // creating an object of integer type
        PriorityQueue queue
            = new PriorityQueue();
 
        // Adding custom input elements
        // using add() method
        queue.add(10);
        queue.add(15);
        queue.add(30);
        queue.add(20);
        queue.add(5);
 
        // Displaying the PriorityQueue
        System.out.println("Initial PriorityQueue: "
                           + queue);
 
        // Removing elements from the PriorityQueue
        // using remove() method
        queue.remove(30);
        queue.remove(5);
 
        // Displaying the PriorityQueue elements
        // after removal
        System.out.println("PriorityQueue after removing "
                           + "elements: " + queue);
    }
}
输出:
Initial PriorityQueue: [5, 10, 30, 20, 15]
PriorityQueue after removing elements: [10, 20, 15]

极客,你有没有想过如果 remove() 方法的调用超过队列中存在的元素会发生什么。在这种情况下,它将继续删除那里的元素,此后它将找不到任何要删除的元素,因此它将抛出异常,如下所示。

例子

Java

// Java Program to illustrate remove() Method
// in PriorityQueue
// Where Exception is encountered
 
// Importing required classes
import java.io.*;
import java.util.PriorityQueue;
 
// Main class
// PriorityQueueException
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an empty PriorityQueue
        PriorityQueue pq
            = new PriorityQueue();
 
        // Note: Elements are inserted in unsorted order in
        // priority queue but after removal of elements
        // queue is always sorted.
 
        // Adding elements in above queue
        // using add() method
        pq.add(2);
        pq.add(14);
        pq.add(41);
        pq.add(7);
        pq.add(99);
 
        // Elements in queue are unsorted by far
 
        // Getting size of above queue before deletion
        // of any element using size() method
        System.out.println(
            "Size of priority queue before deletion : "
            + pq.size());
 
        // Printing all elements of above queue
        System.out.println(
            "Priority queue before removal : " + pq);
 
        // Calling remove() method over priority queue
        // in which there were 5 elements
 
        // Here calling remove() method say be it 2 times
        // So 2 top priority elements will be removed
        System.out.println(" 1st element removed : "
                           + pq.remove());
        System.out.println(" 2nd element removed : "
                           + pq.remove());
        System.out.println(" 3rd element removed : "
                           + pq.remove());
        System.out.println(" 4th element removed : "
                           + pq.remove());
        System.out.println(" 5th element removed : "
                           + pq.remove());
 
        // By now queue is empty and if now we made further
        // remove() call it will throw exception for this
        System.out.println(" 6th element removed : "
                           + pq.remove());
 
        // As we know smaller the integer bigger the
        // priority been set by default comparator of this
        // class
 
        // Note: Now the element is always returned sorted
        // from a priority queue is a trait of this class
 
        // Printing the queue after removal of priority
        // elements
        System.out.println(
            "Priority queue after removal as follows: "
            + pq);
    }
}

输出:

输出说明:

它表明队列中没有其他元素,因为队列现在是空的,所以它会抛出 NoSuchElementException。