logo

Javaキュー

キューは、他のデータ構造と同様に要素を格納するために使用される別の種類の線形データ構造ですが、特定の方法で使用されます。簡単に言うと、キューは、同じ種類の要素を格納する Java プログラミング言語のデータ構造の一種であると言えます。キュー内のコンポーネントは、FIFO (先入れ先出し) 動作で保存されます。キュー コレクションには 2 つの端、つまり前部と後部があります。キューには前と後ろの 2 つの端があります。

次の図は、Java キューの FIFO (先入れ先出し) プロパティを完全に説明しています。

Javaキュー

前の図で説明したように、キューは 2 つの端子、つまり開始 (前) と終了 (後) を持つ線形データ構造であることがわかります。コンポーネントはキューの後端からキュー内に追加され、コンポーネントはキューの前端から抽出されます。

キューは、 ジャワ これは Java.util パッケージに属します。また、Collection インターフェイスも拡張されます。

Java Queue インターフェースの一般的な表現を以下に示します。

 public interface Queue extends Collection 

キューがインターフェイスであることを上で説明したように、インターフェイスをインスタンス化できないため、キューをインスタンス化できないとも言えます。ユーザーが Java で Queue インターフェイスの機能を実装したい場合は、Queue インターフェイスを実装するいくつかの堅牢なクラスが必要です。

Java プログラミング言語には、Queue インターフェイスの実装に使用される 2 つの異なるクラスがあります。これらのクラスは次のとおりです。

Javaキュー

Javaキューの特徴

Java キューは、プログラミングの世界で最も重要なデータ構造の 1 つと考えることができます。 Java Queue の魅力はその特性にあります。 Java キュー データ構造の重要なプロパティは次のとおりです。

  • Java キューは FIFO (先入れ先出し) 方式に従います。これは、要素がキューの最後に入力され、先頭から削除されることを示します。
  • Java Queue インターフェースは、組み込み、削除など、Collection インターフェースのすべてのルールとプロセスを提供します。
  • Queue インターフェイスの実装に使用される 2 つの異なるクラスがあります。これらのクラスは LinkedList と PriorityQueue です。
  • これら 2 つ以外に、Queue インターフェイスを実装するために使用される Array Blocking Queue というクラスがあります。
  • キューには、無制限キューと有界キューの 2 種類があります。 java.util パッケージの一部であるキューは非境界キューとして知られており、境界付きキューは java.util.concurrent パッケージに存在するキューです。
  • Deque または (両端キュー) も、両端からの要素の追加と削除を実行するキューの一種です。
  • また、両端キューはスレッドセーフであると見なされます。
  • ブロッキング キューもスレッドセーフなキューのタイプの 1 つです。ブロッキング キューは、プロデューサーとコンシューマーのクエリを実装するために使用されます。
  • ブロッキング キューは null 要素をサポートしません。ブロッキング キューで、null 値に類似した処理が試行されると、NullPointerException もスローされます。

キューの実装

Queueの実装で使用されるクラス

キューの機能を実装するために使用されるクラスは次のとおりです。

Queueの実装に使用されるインターフェース

Java インターフェイスは、Java キューの実装にも使用されます。キューの機能を実装するために使用されるインターフェイスは次のとおりです。

Javaキュー
  • 何について
  • ブロッキングキュー
  • デキューのブロック
Javaキュー

Javaキュークラスのメソッド

Java キューには、非常に一般的に使用されるメソッドが多数あります。 Queue インターフェイスは、挿入、削除、ピークなどのさまざまなメソッドを促進します。Java キューの一部の操作では例外が発生しますが、これらの操作の一部はプログラムの完了時に特定の値を返します。

注 - Java SE 8 では、Java キューコレクションに変更は加えられません。以下で定義されているこれらのメソッドは、Java プログラミング言語の後継バージョンでさらに用意されています。たとえば、Java SE 9。

Java キューのさまざまなメソッドを以下に定義します。

方法 メソッドのプロトタイプ 説明
追加 ブール値 add(E e) 容量の制限に違反することなく、要素 e をキューの最後尾 (最後尾) に追加します。成功した場合は true を返し、容量が不足した場合は IllegalStateException を返します。
ピーク E ピーク() キューの先頭 (先頭) を削除せずに返します。
要素 E要素() Peak()メソッドと同じ操作を実行します。キューが空の場合は NoSuchElementException をスローします。
取り除く E 削除() キューの先頭を削除して返します。キューが空の場合は NoSuchElementException をスローします。
世論調査 Epoll() キューの先頭を削除して返します。キューが空の場合は null を返します。
オファー ブール値オファー(E e) 容量制限に違反しないように、新しい要素 e をキューに挿入します。
サイズ int サイズ() キュー内の要素のサイズまたは数を返します。

Java キュー配列の実装

キューの実装はスタックの実装ほど単純ではありません。

配列を使用してキューを実装するには、まず n 個の要素を保持する配列を宣言します。

次に、このキューで実行される次の操作を定義します。

1) エンキュー: キューに要素を挿入する操作が Enqueue (プログラムでは関数キュー Enqueue) です。最後に要素を挿入するには、まずキューがいっぱいかどうかを確認する必要があります。いっぱいの場合、要素を挿入できません。リアの場合

2) 尻尾: キューから要素を削除する操作は Dequeue (プログラム内の関数キュー Dequeue) です。まず、キューが空かどうかを確認します。デキュー操作が機能するには、キュー内に少なくとも 1 つの要素が存在する必要があります。

3) フロント: このメソッドはキューの先頭を返します。

4) 表示: このメソッドはキューを走査し、キューの要素を表示します。

Javaキュープログラム

次の Java プログラムは、Queue の実装を示しています。

QueueArrayImplementation.java

 class Queue { private static int front, rear, capacity; private static int queue[]; Queue(int size) { front = rear = 0; capacity = size; queue = new int[capacity]; } // insert an element into the queue static void queueEnqueue(int item) { // check if the queue is full if (capacity == rear) { System.out.printf('
Queue is full
'); return; } // insert element at the rear else { queue[rear] = item; rear++; } return; } //remove an element from the queue static void queueDequeue() { // check if queue is empty if (front == rear) { System.out.printf('
Queue is empty
&apos;); return; } // shift elements to the right by one place uptil rear else { for (int i = 0; i <rear 0 4 - 1; i++) { queue[i]="queue[i" + 1]; } set queue[rear] to if (rear < capacity) decrement rear rear--; return; print queue elements static void queuedisplay() int i; (front="=" rear) system.out.printf('queue is empty
'); traverse front and for (i="front;" i rear; system.out.printf(' %d , ', queue[i]); of queuefront() system.out.printf('
front element the queue: %d', queue[front]); public class queuearrayimplementation main(string[] args) create a capacity q="new" queue(4); system.out.println('initial queue:'); q.queuedisplay(); inserting in q.queueenqueue(10); q.queueenqueue(30); q.queueenqueue(50); q.queueenqueue(70); system.out.println('queue after enqueue operation:'); q.queuefront(); insert q.queueenqueue(90); q.queuedequeue(); system.out.printf('
queue two dequeue operations:'); pre> <p> <strong>Output:</strong> </p> <pre> Initial Queue: Queue is Empty Queue after Enqueue Operation: 10 , 30 , 50 , 70 , Front Element of the queue: 10 Queue is full 10 , 30 , 50 , 70 , Queue after two dequeue operations: 50 , 70 , Front Element of the queue: 50 </pre> <h2>Java Queue Linked List Implementation</h2> <p>As we have implemented the Queue data structure using Arrays in the above program, we can also implement the Queue using Linked List.</p> <p>We will implement the same methods enqueue, dequeue, front, and display in this program. The difference is that we will be using the Linked List data structure instead of Array.</p> <p>The below program demonstrates the Linked List implementation of Queue in Java.</p> <p> <strong>QueueLLImplementation.java</strong> </p> <pre> class LinkedListQueue { private Node front, rear; private int queueSize; // queue size //linked list node private class Node { int data; Node next; } //default constructor - initially front &amp; rear are null; size=0; queue is empty public LinkedListQueue() { front = null; rear = null; queueSize = 0; } //check if the queue is empty public boolean isEmpty() { return (queueSize == 0); } //Remove item from the front of the queue. public int dequeue() { int data = front.data; front = front.next; if (isEmpty()) { rear = null; } queueSize--; System.out.println(&apos;Element &apos; + data+ &apos; removed from the queue&apos;); return data; } //Add data at the rear of the queue. public void enqueue(int data) { Node oldRear = rear; rear = new Node(); rear.data = data; rear.next = null; if (isEmpty()) { front = rear; } else { oldRear.next = rear; } queueSize++; System.out.println(&apos;Element &apos; + data+ &apos; added to the queue&apos;); } //print front and rear of the queue public void print_frontRear() { System.out.println(&apos;Front of the queue:&apos; + front.data + &apos; Rear of the queue:&apos; + rear.data); } } class QueueLLImplementation{ public static void main(String a[]){ LinkedListQueue queue = new LinkedListQueue(); queue.enqueue(6); queue.enqueue(3); queue.print_frontRear(); queue.enqueue(12); queue.enqueue(24); queue.dequeue(); queue.dequeue(); queue.enqueue(9); queue.print_frontRear(); } } </pre> <p> <strong>Output:</strong> </p> <pre> Element 6 added to the queue Element 3 added to the queue Front of the queue:6 Rear of the queue:3 Element 12 added to the queue Element 24 added to the queue Element 6 removed from the queue Element 3 removed from the queue Element 9 added to the queue Front of the queue:12 Rear of the queue:9 </pre> <hr></rear>

Java キューのリンク リストの実装

上記のプログラムでは配列を使用してキューのデータ構造を実装しましたが、リンク リストを使用してキューを実装することもできます。

このプログラムでは、同じメソッド enqueue、dequeue、front、display を実装します。違いは、配列の代わりにリンク リスト データ構造を使用することです。

以下のプログラムは、Java でのキューのリンク リスト実装を示しています。

QueueLLImplementation.java

 class LinkedListQueue { private Node front, rear; private int queueSize; // queue size //linked list node private class Node { int data; Node next; } //default constructor - initially front &amp; rear are null; size=0; queue is empty public LinkedListQueue() { front = null; rear = null; queueSize = 0; } //check if the queue is empty public boolean isEmpty() { return (queueSize == 0); } //Remove item from the front of the queue. public int dequeue() { int data = front.data; front = front.next; if (isEmpty()) { rear = null; } queueSize--; System.out.println(&apos;Element &apos; + data+ &apos; removed from the queue&apos;); return data; } //Add data at the rear of the queue. public void enqueue(int data) { Node oldRear = rear; rear = new Node(); rear.data = data; rear.next = null; if (isEmpty()) { front = rear; } else { oldRear.next = rear; } queueSize++; System.out.println(&apos;Element &apos; + data+ &apos; added to the queue&apos;); } //print front and rear of the queue public void print_frontRear() { System.out.println(&apos;Front of the queue:&apos; + front.data + &apos; Rear of the queue:&apos; + rear.data); } } class QueueLLImplementation{ public static void main(String a[]){ LinkedListQueue queue = new LinkedListQueue(); queue.enqueue(6); queue.enqueue(3); queue.print_frontRear(); queue.enqueue(12); queue.enqueue(24); queue.dequeue(); queue.dequeue(); queue.enqueue(9); queue.print_frontRear(); } } 

出力:

文字列から整数へのJava
 Element 6 added to the queue Element 3 added to the queue Front of the queue:6 Rear of the queue:3 Element 12 added to the queue Element 24 added to the queue Element 6 removed from the queue Element 3 removed from the queue Element 9 added to the queue Front of the queue:12 Rear of the queue:9