首頁  >  文章  >  Java  >  Java中的優先權佇列

Java中的優先權佇列

PHPz
PHPz原創
2024-08-30 16:04:30593瀏覽

一般情況下,Queue中的物件是按照先進先出的順序放置的。即先進先出。在某些情況下,物件必須根據其優先順序進行處理,此時,Java PriorityQueue 就發揮了作用。除此之外,PriorityQueue 還具有某些功能:

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

  • 這是一個無界隊列。
  • 預設情況下,元素的排序是自然順序。
  • 不允許使用空值。
  • 不可比較的物件不能用於建立 PriorityQueue。

Java PriorityQueue 聲明

Java PriorityQueue 可以使用以下語法聲明。

public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable

Java PriorityQueue 的建構子

以下是Java PriorityQueue中常用的建構子:

1。 PriorityQueue(): 建立的 PriorityQueue 容量為 11,預設初始容量為 11。此外,元素是根據自然順序排序的。

2。 PriorityQueue(集合 擴充 E> c): E

> c): 使用上述集合將使用上述情況中的元素創建。 3。 PriorityQueue

(int ic): 將使用提及的初始容量 ic 建立一個 PriorityQueue。此外,元素是根據自然順序排序的。

4。 PriorityQueue(int ic, Comparator super E> comparator): 將使用提到的初始容量 ic 建立一個 PriorityQueue。此外,元素是根據提到的比較器排序的。 5。 PriorityQueue(PriorityQueue extends E中的元素創建。

6。 PriorityQueue(SortedSet 擴充 E> c):E> c):

方法

現在,讓我們來看看Java PriorityQueue中一些常用的方法:

1。 add(E e): 呼叫此方法時,元素 e 將會被加入到 PriorityQueue 中。

2。 size(): 將傳回集合中元素的數量。

3。 clear(): PriorityQueue 中的所有元素都將被刪除。

4。 comparator(): 將傳回用於對佇列進行排序的比較器。如果使用自然排序,將傳回 null。

5。 contains(Objecto): 如果佇列包含提到的元素 o,則傳回 true。

6。迭代器():將傳回對佇列中的元素所使用的迭代器。

7。 Offer(Ee): 所提及的元素 e 將會插入佇列。

8。 peek(): PriorityQueue 的頭部將被檢索,而不是刪除。如果不存在任何元素,將傳回 null。

9。 poll(): PriorityQueue 的頭部將被檢索並刪除。如果不存在任何元素,將傳回 null。

10。刪除(物件o):所提及的元素的單一實例將從佇列中刪除。

11。 toArray(): 將傳回佇列中所有元素的陣列。

12。 toArray(T[] a): 將傳回佇列中所有元素的數組,其中運行時間將與上述數組的運行時間相同。

Java PriorityQueue 範例

下面給的是 Java PriorityQueue 的範例:

範例#1

建立 PriorityQueue 的範例程式。

代碼:

import java.util.Iterator;
import java.util.PriorityQueue;
class Main{
public static void main(String args[]){
//create a PriorityQueue
PriorityQueue<String> q=new PriorityQueue<String>();
//add elements to the queue
q.add("Anna");
q.add("Annamu");
q.add("Adam");
q.add("Iza");
q.add("Thukidi");
System.out.println("Head of the queue:"+q.element());
System.out.println("Head of the queue :"+q.peek());
//Retrieve elements in queue using iterator
System.out.println("Queue elements are :");
Iterator it=q.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
//remove the element from queue
q.remove();
//remove the head of the queue
q.poll();
System.out.println("\n Queue after the removal of 2 elements :");
//Retrieve elements in queue using iterator
Iterator<String> it2=q.iterator();
while(it2.hasNext())
{
System.out.println(it2.next());
}
}
}

輸出:

Java中的優先權佇列

範例程式的工作:

  • 建立一個 PriorityQueue q。
  • 將元素加入佇列。
  • 列印隊列的頭部。
  • 使用迭代器列印佇列中的所有元素。
  • 刪除兩個元素。
  • 移除兩個元素後列印Queue中的所有元素。

範例#2

使用比較器建立 PriorityQueue 的範例程式。

Code:

import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;
class PriorityQueueExample{
public static void main(String[] args) {
//Create a custom comparator. In this, length of 2 strings are getting compared
Comparator<String> cmp = new Comparator<String>()
{
@Override
public int compare(String str1, String str2) {
return str1.length() - str2.length();
}
};
// PriorityQueue creation with Comparator
PriorityQueue<String> q = new PriorityQueue<>(cmp);
// Add elements to the Queue
q.add("Anna");
q.add("Annamu");
q.add("Adam");
q.add("Iza");
q.add("Thukidi");
q.add("Sam");
q.add("Elsa");
q.add("Kukku");
q.add("Mathu");
q.add("Radha");
// Remove elements from the Queue
while (!q.isEmpty()) {
System.ou<em>t</em>.println(q.remove());
}
}
}

Output:

Java中的優先權佇列

Working of the sample program:

  • Create a custom comparator where the elements in the Queue are compared based on the length.
  • Create a PriorityQueue q.
  • Add elements to the Queue.
  • Remove the elements from the queue based on the length of the strings.
  • Print the elements in the queue in the order of removal.

Example #3

Sample program to implement a PriorityQueue by making use of different methods.

Code:

import java.util.*;
class Main{
public static void main(String args[]){
//create a PriorityQueue
PriorityQueue<String> q=new PriorityQueue<String>();
//add elements to the queue
q.add("Anna");
q.add("Annamu");
q.add("Adam");
q.add("Iza");
q.add("Thukidi");
System.out.println("Head of the queue:"+q.element());
System.out.println("Head of the queue :"+q.peek());
//Retrieve elements in queue using iterator
System.out.println("Queue elements are :");
Iterator it=q.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
//remove the element from queue
q.remove();
//remove the head of the queue
q.poll();
// Check whether the element Anna is present in queue using the method Contains()
boolean b = q.contains("Anna");
System.out.println("Is there any element Anna in the PriorityQueue ? " + b);
//Check whether the element Iza is present in queue using the method Contains()
boolean bl = q.contains("Iza");
System.out.println("Is there any element Anna in the PriorityQueue ? " + bl);
}
}

Output:

Java中的優先權佇列

Working of the sample program:

  • Create a PriorityQueue q.
  • Add elements and Print the head of the queue.
  • Print all the elements in the Queue using iterator.
  • Remove two elements using the methods remove() and poll().
  • Check whether the two elements Anna and Iza are available in Queue using the method Contains().

以上是Java中的優先權佇列的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java佇列介面下一篇:Java佇列介面