[Java] 13.3. Queue

Queue,以佇列的模式排隊管理你的資料。

 

image/svg+xmlPriorityQueue 類別提供了製作 最可憐考績排⾏榜的功能 13.3.Queue 殭屍每次吃的食物都不⼀定, 如何讓殭屍吃不定個數的食物 ? import java.util.*; class Main{ public static void main( String [] args){ TreeSet scores = new TreeSet (); scores.add ( 3 ); scores.add ( 5 ); scores.add ( 2 ); scores.add ( 2 ); scores.add ( 4 ); for ( Object o : scores){ System.out.println(o); } } } 執⾏結果 2 2 3 4 5 import java.util.*; class Main{ public static void main( String [] args){ PriorityQueue scores = new PriorityQueue (); scores.offer ( 3 ); scores.offer ( 5 ); scores.offer ( 2 ); scores.offer ( 2 ); scores.offer ( 4 ); Object s = null ; while ( ( s = scores.poll ()) != null ){ System.out.println( s ); } } } 執⾏結果 2 2 3 4 5 import java.util.*; class Main{ public static void main( String [] args){ PriorityQueue scores = new PriorityQueue (); scores.offer ( 3 ); scores.offer ( 5 ); scores.offer ( 2 ); scores.offer ( 2 ); scores.offer ( 4 ); System.out.println( scores.peek ()); System.out.println( scores.peek ()); System.out.println( scores.peek ()); System.out.println( scores.peek ()); System.out.println( scores.peek ()); System.out.println( scores.peek ()); } } 執⾏結果 2 2 2 2 2 2 import java.util.*; class Main{ public static void main( String [] args){ LinkedList schedule = new LinkedList (); schedule.offer ( "Jack" ); schedule.offer ( "Jason" ); schedule.offer ( "Mary" ); schedule.offer ( "Eric" ); System.out.println( schedule.peek ()); System.out.println( schedule.peek ()); Object s = null ; while ( (s = schedule.poll ()) != null ){ System.out.println(s); } } } 執⾏結果 Jack Jack Jack Jason Mary Eric import java.util.*; class Main{ public static void main( String [] args){ PriorityQueue scores = new PriorityQueue (); scores.offer ( 3 ); scores.offer( 5 ); scores.add ( 2 ); scores.add( 2 ); scores.add( 4 ); Iterator i = scores.iterator (); while ( i.hasNext () ){ System.out.println( i.next ()); } Object s= null ; while ((s = scores.poll ()) != null ){ System.out.println(s); } } } 執⾏結果 2 2 3 5 4 2 2 3 4 5 J13_3_1 Main.java 2. 不以資料⼤⼩的順序,將考績資料加入⾄考績排⾏榜中, 希望介⾯ TreeSet 類別的能⼒,將資料由⼩排到⼤。 J13_3_2 Main.java 1. 使⽤ TreeSet 類別,希望能夠得到資料排序的功能,以製作考績排⾏榜。 3. 應該還有 1 2 分考績的考績資料才對吧!但怎 麼不⾒了?看來 TreeSet 除了有排程的功能外,還擁 Set 的特性,會將相同重複的排除。 1. 我們將使⽤ PriorityQueue 類別。 2. 使⽤ PriorityQueue 類別,希望能夠得到資料排序的功能。 3. 不以資料⼤⼩的順序,將考績資料加入⾄考績排⾏榜中,希望介⾯ TreeSet 類別 的能⼒,將資料由⼩排到⼤,其中也有相同考績分類的資料存在著。 4. 將取出的資料放入 s 變數中。沒 錯,這是⼀個物件,它已經被⾃動包 裝會 Integer 的物件;當無資料時 poll() ⽅洲會回傳 null 3. 使⽤ poll() ⽅法將考績資料取出,同時 poll() ⽅法也會將取出的資料從集合中移除。 6. s 變數不為 null 時,表⽰有取得資料。 7. 將取得的考績資料直接印出來。 8. 嗯~終於完成了最可憐考績排⾏榜了,在這邊最差的考績 (分數最少的)會從到最上⽅,並由⼩到⼤排列出考績資料。 J13_3_3 Main.java 1. 建立 PriorityQueue 物件,模擬考績排⾏ 榜,同時不以資料⼤⼩的順序加入考績資料。 2. 嘗試進⾏ 6 次的 peek 動作。 3. 不論 peek() 多少次,取得的還是最上層的 2 的考績。原因是 peek() ⽅法因為沒有將最上層的資料移除,⽽只作取出的動作, 因此每次 peek() 的時候,還是會取出原本 2 的考績資料。 J13_3_4 Main.java 2. 只要是實作 Queue 介⾯的類別,皆會提供 offer() ⽅法, 現在使⽤ offer() ⽅法將員⼯的姓名資料放入以排定班表。 1. 建立 LinkedList 物件以模擬班表。 3. 只要是實作 Queue 介⾯的類別,皆會提供 peek() ⽅法以在不移 除資料的情況下,拿出檔頭的資料。在 LinkedList 中使⽤ peek() 就是取出最先放入的資料。因此可以拿出最先加入的員⼯姓名。 4. 只要是實作 Queue 介⾯的類別,皆會提供 poll() ⽅法, 透過此⽅法可將存入的員⼯姓名拿出後,並將資料移除。 5. 這是 Peek() ⽅法取出的資料,它不會作移除的 動作,因此會出現最先放入的資料 Jack 兩次。 6. 使⽤ LinkedList 製作的班表⼀定保證資料順序就 是放入的順序;先放入的資料⼀定會先被取出來。 J13_3_5 Main.java 2. 透過 Collection 介⾯的 iterator() ⽅法,取得 Iterator 物件。其所取出的資料並不會如預期的是由⼩排到⼤的。 3. 因為排序的功能是由 Queue 介⾯所制定的,因此若 要使⽤ PriorityQueue 由⼩排到⼤的功能,必須使⽤ Queue 介⾯的 poll() ⽅法來取得資料。 1. 在放入資料時,不論是透過 Queue 介⾯的 offer() ⽅法,或是透過 Collection 介⾯的 add() ⽅法,皆不 會影響 PriorityQueue 排序資料的功能。 LinkedQueue 也是⼀樣,若要正確的依照 LinkedQueue 所制定的排序規 則,必須使⽤ Queue 介⾯所提供的⽅法 poll() 來取得資料,如此才能正確的 享有 LinkedQueue 排序資料規則。 1 1 peek() ⽅法在不移除考績的情況下, 幫您拿出最可憐的考績 1 1 LinkedList " 先進先出 " 模式也適合排定班表 1 1 1 1 2 2 提供特定資料排序服務是 Queue 介⾯, 不是 Collection 介⾯ 1 2 1 2

留言