首頁 > Java > java教程 > 主體

Java中的佇列

WBOY
發布: 2024-08-30 16:03:40
原創
983 人瀏覽過

佇列資料結構採用先進先出(FIFO)原則。它用於按照到達順序保存要處理的物件;這與排隊的人非常相似。由於Java以Collection介面的形式提供了對資料結構的大量支持,因此佇列是Collection介面中可用的介面。它擴充了 Collection 介面。它在 Java.util 套件中提供,並支援 Collection 介面中可用的所有操作,以及一些額外的提取、插入和檢查操作。

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

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

文法:

Interface Queue<E>
登入後複製

佇列是一個接口,而不是一個類,因此不能直接實例化。該聲明表明隊列接受類似於集合的通用值,並且我們可以將任何物件傳遞給它。 Java有多種Queue介面的實現,我們在使用Queue時可以使用它們。它們是 LinkedList 和 PriorityQueue。

隊列可以聲明如下:

Queue< Object > q = new LinkedList<>();
登入後複製
Queue< Object > q = new PriorityQueue<>();
登入後複製

Java 中佇列是如何運作的?

  • 如前所述,Queue 介面擴展了集合介面;因此,它支援其中可用的所有基本操作方法。
  • LinkedList 和 PriorityQueue 等實作實作了 Queue 介面中宣告的佇列特定方法。
  • LinkedList 依照 LinkedList 的標準儲存元素,即依照插入順序。 PriorityQueue 維護插入元素的自然順序。
  • 請注意,這兩個實作都不是線程安全的,為此,Java 提供了另一個名為 PriorityBlockingQueue 的實現,它是線程安全的。

Java 中的佇列成員類型

  • 由於佇列是一個接口,因此它只包含抽象方法,沒有資料成員。
  • 佇列僅提供一種定義在子類別中實作的操作的方法。

Java 中的佇列函數

  • 由於佇列支援 FIFO 結構,因此它允許從一端插入元素並從另一端(前端)刪除元素。
  • 這是佇列支援的兩個基本操作。
  • 佇列中所有可用的方法可以分為兩類;第一種方法在操作失敗時拋出異常,例如找不到元素,而在第二種方法中,在操作失敗時傳回任何特定值(例如null 或false),而不是異常.
  • 佇列概念中的頭始終代表佇列中的第一個元素;刪除時,該頭元素將首先被刪除。

以下是所有在佇列中可用的方法:

  Returns special value Throws exception
Insert offer(e) add(e)
Remove poll() remove()
Examine peek() element()
  傳回特殊值 拋出異常 插入 報價(e) 新增(e) 刪除 民意調查() 刪除() 檢查 窺視() 元素() 表>

So as explained, two types of methods throw an exception and return a special value. There are three types of operation in this kind of operation: insertion, the second is removal, and the third is retrieval or examination. In the case of the remove operation, an object will be removed from the queue. Still, in the case of examination, the object will be returned without actually removing from the queue.

Examples of Queue in Java

Given below are the different examples of Queue in Java:

Example #1 – Add operation with LinkedList implementation

Code:

import java.util.LinkedList;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
}
}
登入後複製

Output:

Java中的佇列

Note here that the order of insertion is the same with output from left to write.

Example #2 – Let’s remove the added elements one by one

Code:

import java.util.LinkedList;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
while (!q.isEmpty()) {
System.out.print(q.remove() + " ");
}
System.out.println("");
System.out.println(q);
}
}
登入後複製

Output:

Java中的佇列

Here, we have used the function isEmpty() to check when the queue becomes empty after removing elements. The removal order is the same as per the insertion. After removing all the elements, we printed the queue and obtained an empty bracket at the end.

Example #3 – Insertion and Removal Operation on PriorityQueue

Code:

import java.util.PriorityQueue;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new PriorityQueue<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
while (!q.isEmpty()) {
System.out.print(q.remove() + " ");
}
System.out.println("");
System.out.println(q);
}
}
登入後複製

Output:

Java中的佇列

Here, we have used PriorityQueue, which will hold and return the elements depending upon the elements’ natural ordering or upon the comparator, if any passed. Note the insertion order and removal orders are not the same. The removal is based totally on the value of elements.

Example #4 – Examine operation on LinkedList

Code:

import java.util.LinkedList;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
System.out.println( q.peek() );
System.out.println(q);
}
}
登入後複製

Output:

Java中的佇列

Note here that we have used the peek() function, which will return the head of the queue without actually removing it. We printed the queue after performing the peek operation, and you can observe that the head element, which is 5, remains unchanged in the queue.

Example #5 – Examine operation on PriorityQueue

Code:

import java.util.PriorityQueue;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new PriorityQueue<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
System.out.println( q.peek() );
System.out.println(q);
}
}
登入後複製

Output:

Java中的佇列

This is similar to the previous example’s LinkedList operation, but note the head element is 1 because it’s a PriorityQueue.

Conclusion

Java utilizes the Queue interface as a means to maintain elements in insertion order. It supports operations like insertion, retrieval, and removal. There are alternative methods available for all the methods. We have seen examples of the most commonly used methods in queue operation.

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

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!