1. 程式人生 > >數據結構——java Queue類

數據結構——java Queue類

elements 圖例 art queue類 public rop fail success dia

定義


隊列是一種特殊的線性表,它只允許在表的前端進行刪除操作,而在表的後端進行插入操作。

LinkedList類實現了Queue接口,因此我們可以把LinkedList當成Queue來用

圖例


Queue本身是一種先入先出的模型(FIFO),和我們日常生活中的排隊模型很類似。根據不同的實現,他們主要有數組和鏈表兩種實現形式。如下圖:

技術分享圖片

技術分享圖片

與隊列相關的類的關系圖如下:

技術分享圖片

常用方法


序號 方法名 描述
1 boolean add(E e) 將指定的元素插入到隊列中。
2 Object element() 檢索該隊列的頭。
boolean offer(E e) 將指定元素插入到該隊列中(在該隊列容量之內)。
3 Object peek() 檢索該隊列的頭,如果該隊列為空返回null。
4 Object poll() 檢索並刪除該隊列的頭,如果該隊列為空返回null。
5 Object remove() 檢索並刪除該隊列的頭。

源碼


 1 package java.util;
 2 
 3 public interface Queue<E> extends
Collection<E> { 4 /** 5 * Inserts the specified element into this queue if it is possible to do so 6 * immediately without violating capacity restrictions, returning 7 * {@code true} upon success and throwing an {@code IllegalStateException} 8 * if no space is currently available.
9 * 10 * @param e the element to add 11 * @return {@code true} (as specified by {@link Collection#add}) 12 * @throws IllegalStateException if the element cannot be added at this 13 * time due to capacity restrictions 14 * @throws ClassCastException if the class of the specified element 15 * prevents it from being added to this queue 16 * @throws NullPointerException if the specified element is null and 17 * this queue does not permit null elements 18 * @throws IllegalArgumentException if some property of this element 19 * prevents it from being added to this queue 20 */ 21 boolean add(E e); 22 23 /** 24 * Inserts the specified element into this queue if it is possible to do 25 * so immediately without violating capacity restrictions. 26 * When using a capacity-restricted queue, this method is generally 27 * preferable to {@link #add}, which can fail to insert an element only 28 * by throwing an exception. 29 * 30 * @param e the element to add 31 * @return {@code true} if the element was added to this queue, else 32 * {@code false} 33 * @throws ClassCastException if the class of the specified element 34 * prevents it from being added to this queue 35 * @throws NullPointerException if the specified element is null and 36 * this queue does not permit null elements 37 * @throws IllegalArgumentException if some property of this element 38 * prevents it from being added to this queue 39 */ 40 boolean offer(E e); 41 42 /** 43 * Retrieves and removes the head of this queue. This method differs 44 * from {@link #poll poll} only in that it throws an exception if this 45 * queue is empty. 46 * 47 * @return the head of this queue 48 * @throws NoSuchElementException if this queue is empty 49 */ 50 E remove(); 51 52 /** 53 * Retrieves and removes the head of this queue, 54 * or returns {@code null} if this queue is empty. 55 * 56 * @return the head of this queue, or {@code null} if this queue is empty 57 */ 58 E poll(); 59 60 /** 61 * Retrieves, but does not remove, the head of this queue. This method 62 * differs from {@link #peek peek} only in that it throws an exception 63 * if this queue is empty. 64 * 65 * @return the head of this queue 66 * @throws NoSuchElementException if this queue is empty 67 */ 68 E element(); 69 70 /** 71 * Retrieves, but does not remove, the head of this queue, 72 * or returns {@code null} if this queue is empty. 73 * 74 * @return the head of this queue, or {@code null} if this queue is empty 75 */ 76 E peek(); 77 }

參考:http://blog.csdn.net/u010617952/article/details/51726789

數據結構——java Queue類