logo

Java のキュー インターフェイス

キュー インターフェイスは、 java.util パッケージ化して拡張します コレクションインターフェース 。データは、後方に要素が追加され、前方から要素が削除される順序で保存および処理されます。

主な特長

  • FIFO 順序: 要素は挿入された順序 (先入れ先出し) で処理されます。
  • ランダムアクセスなし: List 要素とは異なり、インデックスによって直接アクセスすることはできません。
  • 複数のバリエーション: PriorityQueue Deque ArrayDeque および LinkedList の実装が含まれます。
  • 2 つのメソッドセット: スロー例外バージョン (要素の追加) と安全なバージョン (ポーリング ピークの提供)。

Javaキューインターフェースの宣言

Queue インターフェースは次のように宣言されます。

パブリックインターフェイスキューはコレクションを拡張します



Queue はインターフェイスであるため、直接インスタンス化することはできません。ここでは、このインターフェイスを実装する LinkedList や PriorityQueue などのクラスを使用できます。

キュー = 新しいリンクリスト();

Javaの例外処理とは何ですか

それでは、最初に簡単な例を見てから、記事の内容を詳しく見ていきましょう。

例: LinkedList を使用した基本的なキュー

Java
import java.util.LinkedList; import java.util.Queue; public class Geeks {    public static void main(String args[])   {  // Create a Queue of Integers using LinkedList  Queue<Integer> q = new LinkedList<>();    System.out.println('Queue elements: ' + q);  } } 

出力
Queue elements: [] 

キューはインターフェイスであるため、宣言用の具象クラスが必要です。最も一般的なクラスは次のとおりです。 プルリティキュー そして リンクリスト ジャワでは。これらの実装はどちらもスレッドセーフではないことに注意してください。 優先ブロッキングキュー スレッドセーフな実装が必要な場合の代替実装の 1 つです。

10/100.00

キューオブジェクトの作成

キューはインターフェイスであるため、キュー タイプのオブジェクトを作成することはできません。オブジェクトを作成するには、このリストを拡張するクラスが常に必要です。そして導入後も ジェネリック Java 1.5 では、キューに格納できるオブジェクトのタイプを制限できます。このタイプセーフなキューは次のように定義できます。

Java
// Obj is the type of the object to be stored in Queue Queue<Obj> queue = new PriorityQueue<Obj> (); 

一般的な方法

Queue インターフェイスには、キューに要素を追加、削除、検査するためのメソッドがいくつか用意されています。最も一般的に使用される方法のいくつかを次に示します。

  • 追加(要素) : キューの後ろに要素を追加します。キューがいっぱいの場合は例外がスローされます。
  • オファー(要素): 要素をキューの後ろに追加します。キューがいっぱいの場合は false を返します。
  • 取り除く() : キューの先頭にある要素を削除して返します。キューが空の場合は例外がスローされます。
  • ポーリング(): キューの先頭にある要素を削除して返します。キューが空の場合は null を返します。
  • 要素(): キューの先頭にある要素を削除せずに返します。キューが空の場合は例外がスローされます。
  • ピーク() : キューの先頭の要素を削除せずに返します。キューが空の場合は null を返します。

例 1: この例では、基本的なキュー操作を示します。

Java
import java.util.LinkedList; import java.util.Queue; public class Geeks {  public static void main(String[] args) {  Queue<String> queue = new LinkedList<>();  // add elements to the queue  queue.add('apple');  queue.add('banana');  queue.add('cherry');  System.out.println('Queue: ' + queue);  // remove the element at the front of the queue  String front = queue.remove();  System.out.println('Removed element: ' + front);  // print the updated queue  System.out.println('Queue after removal: ' + queue);  // add another element to the queue  queue.add('date');  // peek at the element at the front of the queue  String peeked = queue.peek();  System.out.println('Peeked element: ' + peeked);  // print the updated queue  System.out.println('Queue after peek: ' + queue);  } } 

出力
Queue: [apple banana cherry] Removed element: apple Queue after removal: [banana cherry] Peeked element: banana Queue after peek: [banana cherry date] 

例 2 :

Java
import java.util.LinkedList; import java.util.Queue; public class Geeks {  public static void main(String[] args){  Queue<Integer> q = new LinkedList<>();  // Adds elements {0 1 2 3 4} to the queue  for (int i = 0; i < 5; i++)  q.add(i);  // Display contents of the queue  System.out.println('Elements of queue: ' + q);  // To remove the head of queue  int removedele = q.remove();    System.out.println('Removed element:'+ removedele);  System.out.println(q);  // To view the head of queue  int head = q.peek();  System.out.println('Head of queue:'+ head);  // Rest all methods of collection interface like size and contains can be used with this implementation.  int size = q.size();  System.out.println('Size of queue:' + size);  } } 

出力
Elements of queue: [0 1 2 3 4] Removed element:0 [1 2 3 4] Head of queue:1 Size of queue:4 

キューインターフェイスを実装するクラス

1.優先キュー

PriorityQueue クラスを使用すると、通常のキューの通常の FIFO 順序ではなく、優先順位に基づいて要素を処理できます。要素を優先順位に従って処理する必要がある場合に便利です。このクラスを使用してキューを作成する方法は次のとおりです。

例:

jsではnullではありません
Java
import java.util.*; class Geeks {  public static void main(String args[]){  // Creating empty priority queue  Queue<Integer> pq = new PriorityQueue<Integer>();  // Adding items to the pQueue using add()  pq.add(10);  pq.add(20);  pq.add(15);  // Printing the top element of the PriorityQueue  System.out.println(pq.peek());  // Printing the top element and removing it the PriorityQueue container  System.out.println(pq.poll());  // Printing the top element again  System.out.println(pq.peek());  } } 

出力
10 10 15 

2. リンクリスト

LinkedList は線形データ構造であり、要素が個別のオブジェクトとして格納され、各オブジェクトにはデータと次の要素へのリンクが含まれます。要素は、連続メモリに格納されていないポインタを使用して接続されます。このクラスを使用してキューを作成する方法は次のとおりです。

例:

10mlからオンス
Java
import java.util.*; class Geeks {  public static void main(String args[])  {  // Creating empty LinkedList  Queue<Integer> ll = new LinkedList<Integer>();  // Adding items to the ll using add()  ll.add(10);  ll.add(20);  ll.add(15);  // Printing the top element of the LinkedList  System.out.println(ll.peek());  // Printing the top element and removing it from the LinkedList container  System.out.println(ll.poll());  // Printing the top element again  System.out.println(ll.peek());  } } 

出力
10 10 20 

3.PriorityBlockingQueue

PriorityBlockingQueue は、PriorityQueue のような要素を順序付けし、ブロッキング取得をサポートする、スレッドセーフな無制限のブロッキング キューです。無制限であるため、メモリが不足すると要素の追加が失敗する可能性があります。このクラスを使用してキューを作成する方法は次のとおりです。

例:

Java
import java.util.concurrent.PriorityBlockingQueue; import java.util.*; class Geeks {  public static void main(String args[])  {  // Creating empty priority blocking queue  Queue<Integer> pbq = new PriorityBlockingQueue<Integer>();  // Adding items to the pbq using add()  pbq.add(10);  pbq.add(20);  pbq.add(15);  // Printing the top element of the PriorityBlockingQueue  System.out.println(pbq.peek());  // Printing the top element and removing it from the PriorityBlockingQueue  System.out.println(pbq.poll());  // Printing the top element again  System.out.println(pbq.peek());  } } 

出力
10 10 15 

PriorityQueue クラスを使用したキュー インターフェイスのさまざまな操作

1. 要素の追加

キューに要素を追加するには、 add() メソッド 。広告掲載オーダーは PriorityQueue には保持されません。要素は、デフォルトでは昇順の優先順位に基づいて保存されます。 

例:

Java
import java.util.*; public class Geeks {  public static void main(String args[])  {  Queue<String> pq = new PriorityQueue<>();  pq.add('Geeks');  pq.add('For');  pq.add('Geeks');  System.out.println(pq);  } } 

出力
[For Geeks Geeks] 

2. 要素の削除

キューから要素を削除するには、 削除()メソッド。 複数のオブジェクトがある場合は、最初に出現したオブジェクトが削除されます。の poll() メソッド ヘッドの取り外しと戻しにも使用します。 

例:

Java
import java.util.*; public class Geeks {  public static void main(String args[])  {  Queue<String> pq = new PriorityQueue<>();  pq.add('Geeks');  pq.add('For');  pq.add('Geeks');  System.out.println('Initial Queue: ' + pq);  pq.remove('Geeks');  System.out.println('After Remove: ' + pq);  System.out.println('Poll Method: ' + pq.poll());  System.out.println('Final Queue: ' + pq);  } } 

出力
Initial Queue: [For Geeks Geeks] After Remove: [For Geeks] Poll Method: For Final Queue: [Geeks] 

3. キューの反復処理

キューを反復処理するには複数の方法があります。最も有名な方法は、キューを配列に変換し、 for ループ 。キューには、キューを反復処理するために使用できる組み込みイテレータもあります。 

例:

文字列から最後の文字を削除する
Java
import java.util.*; public class Geeks {  public static void main(String args[])  {  Queue<String> pq = new PriorityQueue<>();  pq.add('Geeks');  pq.add('For');  pq.add('Geeks');  Iterator iterator = pq.iterator();  while (iterator.hasNext()) {  System.out.print(iterator.next() + ' ');  }  } } 

出力
For Geeks Geeks 

キューインターフェースのメソッド

キューの完全なメソッドリストは次のとおりですJava のインターフェースと Collection から継承するすべてのメソッドそして反復可能

方法

説明

ブール値 add(E e)要素を挿入します。いっぱいの場合は例外をスローします。
ブール値オファー(E e)要素を挿入します。いっぱいの場合は false を返します。
E 削除()ヘッドを取り外します。空の場合は例外をスローします。
E 投票 ()ヘッドを取り外します。空の場合は null を返します。
そして要素()頭を回収します。空の場合は例外をスローします。
E ピーク()頭を回収します。空の場合は null を返します。
ブール値 addAll(コレクションc)別のコレクションからすべての要素を追加します。
ボイドクリア()すべての要素を削除します。
ブール値には (オブジェクト o) が含まれます要素が存在するかどうかを確認します。
ブール値 containsAll(コレクションc)すべての要素が存在するかどうかを確認します。
ブール値等しい(オブジェクト o)別のコレクションと比較します。
int ハッシュコード()ハッシュコードを返します。
ブール値 isEmpty()コレクションが空かどうかを確認します。
イテレーターイテレータ()要素のイテレータを返します。
ブール値の削除(オブジェクトo)特定の要素を削除します。
ブール値removeAll(コレクションc)一致する要素をすべて削除します。
ブール値保持All(コレクションc)指定された要素のみを保持します。
int サイズ()要素の数を返します。
オブジェクト[] toArray()要素を配列として返します。
T[] toArray(T[] a)要素を型付き配列として返します。
デフォルト void forEach(Consumerアクション)要素ごとにアクションを実行します。
デフォルト void forEach(Consumerアクション)要素ごとにアクションを実行します。
デフォルトのスプリッテレータスプリッテレータ()スプリッテレータを返します。
デフォルトのストリームストリーム()順次ストリームを返します。
デフォルトのストリームパラレルストリーム()並列ストリームを返します。