從頭認識java-15.6 隊列(Queue)

來源:互聯網
上載者:User

標籤:family   link   first   blog   front   tin   介面   java   err   

這一章節我們來討論一下隊列(Queue)。

1.什麼是隊列?

隊列是一種特殊的線性表,特殊之處在於它僅僅同意在表的前端(front)進行刪除操作,而在表的後端(rear)進行插入操作,和棧一樣。隊列是一種操作受限制的線性表。

 

2.特性

(1)元素是有序的

(2)元素是先進先出

 

3.java裡面的實作類別:Linkedlist和PriorityQueue,兩者之間效能不存在區別,區別的地方是排序的行為。

package com.ray.ch14;import java.util.LinkedList;import java.util.PriorityQueue;import java.util.Queue;public class Test {public static <T> void test(Queue<T> queue, Generator<T> generator,int count) {for (int i = 0; i < count; i++) {queue.add(generator.next());}while (queue.peek() != null) {System.out.print(queue.remove() + " ");}System.out.println();}public static void main(String[] args) {test(new LinkedList<String>(), new MyGenerator(), 10);test(new PriorityQueue<String>(), new MyGenerator(), 10);}}interface Generator<T> {T next();}class MyGenerator implements Generator<String> {private String str = "one two three four five six seven eight nine ten eleven";private int index = 0;@Overridepublic String next() {if (index > str.split(" ").length) {return "";} else {return str.split(" ")[index++];}}}


輸出:

one two three four five six seven eight nine ten
eight five four nine one seven six ten three two

 

4.優先順序隊列

排序對象實現Comparable介面就可以。

package com.ray.ch14;import java.util.PriorityQueue;import java.util.Random;public class Test {private static PriorityQueue<MyClass> priorityQueue = new PriorityQueue<MyClass>();public static PriorityQueue<MyClass> test(int count) {for (int i = 0; i < count; i++) {priorityQueue.add(new MyClass(new Random().nextInt(10)));}return priorityQueue;}public static void main(String[] args) {System.out.println(test(10));}}class MyClass implements Comparable<MyClass> {private int pri = 0;public MyClass(int pri) {this.pri = pri;}@Overridepublic int compareTo(MyClass myClass) {if (this.pri < myClass.pri) {return -1;} else {if (this.pri == myClass.pri) {return 0;} else {return 1;}}}@Overridepublic String toString() {return this.pri + "";}}


輸出:

[0, 1, 3, 3, 2, 5, 5, 6, 6, 7]

5.雙向隊列

特點:能夠在不論什麼一段加入或者刪除元素。

因為在現有的java 裡面沒有實現雙向隊列的介面。可是在Linkedlist裡面事實上已經類比出來了。因此我們使用組合來類比一下。

class Deque<T> {private LinkedList<T> linkedList = new LinkedList<T>();public void addFirst(T t) {linkedList.addFirst(t);}public void addLast(T t) {linkedList.addLast(t);}public void removeFirst() {linkedList.removeFirst();}public void removeLast() {linkedList.removeLast();}}


 

總結:這一章節主要講述隊列的概念、特點。以及優先順序和雙向隊列。

 

這一章節就到這裡,謝謝。

-----------------------------------

檔案夾

 

從頭認識java-15.6 隊列(Queue)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.