• Tags
  •         
  • www.breakyizhan.com
  •    

    Java.util.PriorityQueue.remove() 方法用于从PriorityQueue中删除特定元素。

    句法:

    Priority_Queue.remove(Object O)

    参数:参数O属于PriorityQueue类型,并指定要从PriorityQueue中删除的元素。

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

    下面的程序说明了Java.util.PriorityQueue.remove()方法

    // Java code to illustrate remove()
    import java.util.*;
    
    public class PriorityQueueDemo {
        public static void main(String args[])
        {
            // Creating an empty PriorityQueue
            PriorityQueue<String> queue = new PriorityQueue<String>();
    
            // Use add() method to add elements into the Queue
            queue.add("Welcome");
            queue.add("To");
            queue.add("Geeks");
            queue.add("For");
            queue.add("Geeks");
    
            // Displaying the PriorityQueue
            System.out.println("Initial PriorityQueue: " + queue);
    
            // Removing elements using remove() method
            queue.remove("Geeks");
            queue.remove("For");
            queue.remove("Welcome");
    
            // Displaying the PriorityQueue after removal
            System.out.println("PriorityQueue after removing "
                               + "elements: " + queue);
        }
    }
    

    输出:

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

    程序2:

    // Java code to illustrate remove()
    import java.util.*;
    
    public class PriorityQueueDemo {
        public static void main(String args[])
        {
            // Creating an empty PriorityQueue
            PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
    
            // Use add() method to add elements into the Queue
            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 using remove() method
            queue.remove(30);
            queue.remove(5);
    
            // Displaying the PriorityQueue after removal
            System.out.println("PriorityQueue after removing "
                               + "elements: " + queue);
        }
    }
    

    输出:

    Initial PriorityQueue: [5, 10, 30, 20, 15]
    PriorityQueue after removing elements: [10, 20, 15]

     
    转载请保留页面地址:https://www.breakyizhan.com/java/5362.html