java佇列(Queue)用法總結
阿新 • • 發佈:2019-01-02
1.佇列的特點
佇列是一種比較特殊的線性結構。它只允許在表的前端(front)進行刪除操作,而在表的後端(rear)進行插入操作。進行插入操作的端稱為隊尾,進行刪除操作的端稱為隊頭。
佇列中最先插入的元素也將最先被刪除,對應的最後插入的元素將最後被刪除。因此佇列又稱為“先進先出”(FIFO—first in first out)的線性表,與棧(FILO-first in last out)剛好相反。
2.java中的佇列
java中的Queue介面就實現了佇列的功能。
public interface Queue<E> extends Collection<E> { /** * Inserts the specified element into this queue if it is possible to do so * immediately without violating capacity restrictions, returning * {@code true} upon success and throwing an {@code IllegalStateException} * if no space is currently available. * * @param e the element to add * @return {@code true} (as specified by {@link Collection#add}) * @throws IllegalStateException if the element cannot be added at this * time due to capacity restrictions * @throws ClassCastException if the class of the specified element * prevents it from being added to this queue * @throws NullPointerException if the specified element is null and * this queue does not permit null elements * @throws IllegalArgumentException if some property of this element * prevents it from being added to this queue */ boolean add(E e); /** * Inserts the specified element into this queue if it is possible to do * so immediately without violating capacity restrictions. * When using a capacity-restricted queue, this method is generally * preferable to {@link #add}, which can fail to insert an element only * by throwing an exception. * * @param e the element to add * @return {@code true} if the element was added to this queue, else * {@code false} * @throws ClassCastException if the class of the specified element * prevents it from being added to this queue * @throws NullPointerException if the specified element is null and * this queue does not permit null elements * @throws IllegalArgumentException if some property of this element * prevents it from being added to this queue */ boolean offer(E e); /** * Retrieves and removes the head of this queue. This method differs * from {@link #poll poll} only in that it throws an exception if this * queue is empty. * * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ E remove(); /** * Retrieves and removes the head of this queue, * or returns {@code null} if this queue is empty. * * @return the head of this queue, or {@code null} if this queue is empty */ E poll(); /** * Retrieves, but does not remove, the head of this queue. This method * differs from {@link #peek peek} only in that it throws an exception * if this queue is empty. * * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ E element(); /** * Retrieves, but does not remove, the head of this queue, * or returns {@code null} if this queue is empty. * * @return the head of this queue, or {@code null} if this queue is empty */ E peek(); }
不得不說,JDK裡的程式碼以及註釋看著就是很舒服。各位稍微花點時間看看JDK裡的原始碼以及註釋,相信會有很多收穫。
3.測試Queue介面
JDK中,LinkedList類實現了Queue介面,可以當Queue使用。
public class QueueTest { @Test public void test() { Queue<Integer> queue = new LinkedList<>(); queue.offer(1); queue.offer(2); queue.offer(3); queue.offer(4); for(int e : queue) { System.out.println(e); } System.out.println("----------"); System.out.println("poll : " + queue.poll()); System.out.println("----------"); for(int e : queue) { System.out.println(e); } System.out.println("ele is: " + queue.element()); System.out.println("----------"); for(int e : queue) { System.out.println(e); } System.out.println("peek : " + queue.peek()); System.out.println("----------"); for(int e : queue) { System.out.println(e); } } }
最終程式碼執行的結果:
1
2
3
4
----------
poll : 1
----------
2
3
4
ele is: 2
----------
2
3
4
peek : 2
----------
2
3
4
結合第二部分內容,可以有以下結論:
1.儘量使用offer()方法新增元素,使用poll()方法移除元素。dd()和remove()方法在失敗的時候會丟擲異常。
2.peek方法不會刪除元素, Retrieves, but does not remove, the head of this queue,