ホームページ  >  記事  >  Java  >  JavaのPriorityQueue

JavaのPriorityQueue

PHPz
PHPzオリジナル
2024-08-30 16:04:30596ブラウズ

通常、キュー内のオブジェクトは FIFO の順序で配置されます。つまり、先入れ先出しです。場合によっては、オブジェクトをその優先度に基づいて処理する必要があるため、そのときに Java PriorityQueue が機能します。それに加えて、PriorityQueue には次のような機能があります。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

  • これは無制限のキューです。
  • デフォルトでは、要素の順序は自然な順序です。
  • Null 値は許可されません。
  • 比較できないオブジェクトは、PriorityQueue の作成に使用できません。

Java PriorityQueue の宣言

Java PriorityQueue は、以下の構文を使用して宣言できます。

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

Java PriorityQueue のコンストラクター

Java PriorityQueue で一般的に使用されるコンストラクターは次のとおりです。

1. PriorityQueue(): PriorityQueue は、初期容量がデフォルトであるため、11 で作成されます。さらに、要素は自然な順序に基づいて順序付けされます。

2. PriorityQueue(CollectionE> c): A PriorityQueue は言及されたコレクションの要素を使用して作成されました。

3. PriorityQueue(int ic): PriorityQueue は、指定された初期容量 ic で作成されます。さらに、要素は自然な順序に基づいて順序付けされます。

4. PriorityQueue(int ic, Comparator comparator): PriorityQueue は、指定された初期容量 ic で作成されます。さらに、要素は前述のコンパレーターに基づいて順序付けされます。

5. PriorityQueue(PriorityQueueE> c): PriorityQueue は前述の PriorityQueue の要素を使用して作成されます。

6. PriorityQueue(SortedSetE> c): 前述のソートセット内の要素を使用して PriorityQueue が作成されます。

メソッド

ここで、Java PriorityQueue で一般的に使用されるメソッドをいくつか見てみましょう。

1. add(E e): このメソッドの呼び出し時に要素 e が PriorityQueue に追加されます。

2. size(): コレクション内の要素の数が返されます。

3. clear():PriorityQueue 内のすべての要素が削除されます。

4. comparator(): キューのソートに使用されるコンパレータが返されます。自然順序付けが使用される場合は、null が返されます。

5. contains(Objecto): キューに言及された要素 o が含まれる場合、true が返されます。

6. iterator(): キュー内の要素に対して使用されるイテレータが返されます。

7. offer(Ee): メンションされた要素 e がキューに挿入されます。

8. Peak():PriorityQueue の先頭は削除されずに取得されます。要素が存在しない場合は、null が返されます。

9. poll(): PriorityQueue の先頭が取得され、削除されます。要素が存在しない場合は、null が返されます。

10. Remove(Objecto):言及された要素の 1 つのインスタンスがキューから削除されます。

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

サンプルプログラムの動作:

  • PriorityQueue を作成します。
  • 要素をキューに追加します。
  • キューの先頭を出力します。
  • 反復子を使用してキュー内のすべての要素を出力します。
  • 2 つの要素を削除します。
  • 2 つの要素を削除した後、キュー内のすべての要素を出力します。

例 #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のPriorityQueue

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のPriorityQueue

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のPriorityQueueの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。