java集合類——Stack棧類與Queue隊列


Stack繼承Vector類,它通過五個操作對類 Vector 進行了擴展。 棧是 后進先出的。 棧提供了通常的 pushpop 操作,以及取堆棧頂點的 peek 方法、測試堆棧是否為空的 empty 方法、在堆棧中查找項並確定到堆棧頂距離的 search 方法。

 

方法摘要
 boolean empty()
          測試堆棧是否為空。
 E peek()
          查看堆棧頂部的對象,但不從堆棧中移除它。
 E pop()
          移除堆棧頂部的對象,並作為此函數的值返回該對象。
 E push(E item)
          把項壓入堆棧頂部。
 int search(Object o)
          返回對象在堆棧中的位置,以 1 為基數。

現附上例子,后續繼續總結

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
  * @作者 whs
  * @創建日期 2015年2月4日
  * @版本 V 1.0
  */
package thread.pool;
 
import java.util.Stack;
 
public class StackExam {
     
     public static void main(String[] args) {
         Stack<String> stack = new Stack<String>();
         System.out.println( "now the satck is " +isEmpty(stack));
         stack.push( "1" );
         stack.push( "2" );
         stack.push( "3" );
         stack.push( "4" );
         stack.push( "5" );
         stack.push( "6" );
         System.out.println( "now the stack is " +isEmpty(stack));
         System.out.println(stack.peek()); //查看堆棧頂部的對象,並返回該對象,但不從堆棧中移除它。
         System.out.println(stack.pop());
         System.out.println(stack.pop());
         System.out.println(stack.search( "3" )); //,此方法返回最近的目標對象距堆棧頂部出現位置到堆棧頂部的距離;
     }
     
     public static String isEmpty(Stack<String> stack){
         return stack.empty() ? "empty" : "not empty" ;
     }  
}

 輸出為:

1
2
3
4
5
6
now the satck is empty
now the stack is not empty
6
6
5
2

接口 Queue隊列:

    Queue接口與List、Set同一級別,都是繼承了Collection接口。LinkedList實現了Queue接 口。Queue接口窄化了對LinkedList的方法的訪問權限(即在方法中的參數類型如果是Queue時,就完全只能訪問Queue接口所定義的方法 了,而不能直接訪問 LinkedList的非Queue的方法),以使得只有恰當的方法才可以使用。BlockingQueue 繼承了Queue接口。

      隊列通常(但並非一定)以 FIFO(先進先出)的方式排序各個元素。不過優先級隊列和 LIFO 隊列(或堆棧)例外,前者根據提供的比較器或元素的自然順序對元素進行排序,后者按 LIFO(后進先出)的方式對元素進行排序。無論使用哪種排序方式,隊列的 都是調用 remove()poll() 所移除的元素。在 FIFO 隊列中,所有的新元素都插入隊列的末尾。其他種類的隊列可能使用不同的元素放置規則。每個 Queue 實現必須指定其順序屬性。

方法摘要
 boolean add(E e)
          將指定的元素插入此隊列(如果立即可行且不會違反容量限制),在成功時返回 true,如果當前沒有可用的空間,則拋出 IllegalStateException
 E element()
          獲取,但是不移除此隊列的頭。
 boolean offer(E e)
          將指定的元素插入此隊列(如果立即可行且不會違反容量限制),當使用有容量限制的隊列時,此方法通常要優於 add(E),后者可能無法插入元素,而只是拋出一個異常。
 E peek()
          獲取但不移除此隊列的頭;如果此隊列為空,則返回 null
 E poll()
          獲取並移除此隊列的頭,如果此隊列為空,則返回 null
 E remove()
          獲取並移除此隊列的頭。

       Queue使用時要盡量避免Collection的add()和remove()方法,而是要使用offer()來加入元素,使用poll()來獲取並移出元素。它們的優點是通過返回值可以判斷成功與否,add()和remove()方法在失敗的時候會拋出異常。 如果要使用前端而不移出該元素,使用element()或者peek()方法。

       注意:poll和peek方法出錯進返回null。因此,向隊列中插入null值是不合法的。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Queue<String> queue= new LinkedList<String>();
queue.offer( "Hello" );
queue.offer( "World!" );
queue.offer( "你好?" );
System.out.println(queue.size());
for (String str:queue){
     System.out.printf(str + "  " );
}
System.out.printf( "\n" );
System.out.println(queue.size());
String str;
while ((str=queue.poll()) != null ){
     System.out.printf(str + "  " );
}
System.out.println();
System.out.println(queue.size());

 輸出結果:

1
2
3
4
5
3
Hello  World !  你好? 
3
Hello  World !  你好? 
0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM