Java PriorityQueue remove() 方法



描述

java PriorityQueue remove() 方法檢索並移除由該佇列表示的佇列的頭部

宣告

以下是 java.util.PriorityQueue.remove() 方法的宣告

public E remove()

引數

返回值

此方法返回由該隊列表示的佇列的頭部

異常

NoSuchElementException - 如果此佇列為空

帶 Object 作為引數的 remove() 方法

描述

Java PriorityQueue remove(Object) 方法從此佇列中移除指定元素的一個例項。

宣告

以下是 java.util.PriorityQueue.remove(o) 方法的宣告

public boolean remove(Object o)

引數

o - 要從此佇列中移除的元素,如果存在

返回值

此方法返回true,如果此佇列包含指定的元素。

異常

從整數 PriorityQueue 中移除第一個元素的示例

以下示例演示了使用 Java PriorityQueue remove() 方法處理整數的情況。我們建立了一個整數 PriorityQueue,添加了一些元素,列印它,然後使用 remove() 方法移除第一個元素。由於 PriorityQueue 已修改,因此列印它以檢查第一個元素是否已移除。

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
      
      // create an empty priority queue
      PriorityQueue<Integer> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add(25);
      queue.add(30);
      queue.add(20);
      queue.add(18);        

      // let us print all the elements available in queue
      System.out.println("PriorityQueue = " + queue);

      // it will retrieve first element after removing from queue
      System.out.println("Retrieved Element is = " + queue.remove());
	  
      // let us print all the elements available in queue again
      System.out.println("PriorityQueue = " + queue);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

PriorityQueue = [18, 20, 25, 30]
Retrieved Element is = 18
PriorityQueue = [20, 30, 25]

從字串 PriorityQueue 中移除第一個元素的示例

以下示例演示了使用 Java PriorityQueue remove() 方法處理字串的情況。我們建立了一個字串 PriorityQueue,添加了一些元素,列印它,然後使用 remove() 方法獲取第一個元素。由於 PriorityQueue 已修改,因此列印它以檢查第一個元素是否已移除。

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
      
      // create an empty priority queue
      PriorityQueue<String> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add("A");
      queue.add("B");
      queue.add("C");
      queue.add("D");        

      // let us print all the elements available in queue
      System.out.println("PriorityQueue = " + queue);

      // it will retrieve first element after removing from queue
      System.out.println("Retrieved Element is = " + queue.remove());
	  
      // let us print all the elements available in queue again
      System.out.println("PriorityQueue = " + queue);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

PriorityQueue = [A, B, C, D]
Retrieved Element is = A
PriorityQueue = [B, D, C]

從物件 PriorityQueue 中移除第一個元素的示例

以下示例演示了使用 Java PriorityQueue remove(object) 方法處理 Student 物件的情況。我們建立了一個 Student PriorityQueue,添加了一些元素,列印它,然後使用 remove(object) 方法移除特定的學生。由於 PriorityQueue 已修改,因此列印它以檢查該學生物件是否已移除。

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
      
      // create an empty priority queue
      PriorityQueue<Student> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add(new Student(1, "Julie"));
      queue.add(new Student(2, "Robert"));
      queue.add(new Student(3, "Adam"));       

      // let us print all the elements available in queue
      System.out.println("PriorityQueue = " + queue);

      // it will return true after removing Robert from queue
      System.out.println("Student removed :" + queue.remove(new Student(2, "Robert")));
	  
      // let us print all the elements available in queue again
      System.out.println("PriorityQueue = " + queue);
   }
}
class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

PriorityQueue = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Student removed : true
PriorityQueue = [[ 1, Julie ], [ 3, Adam ]]
java_util_priorityqueue.htm
廣告