標籤:inter cep alt first max for remove 今天 contains
隊列是Java集合中的重要組成部分,具有先進先出的特性,使其具有廣泛的應用情境,比如排隊等。因此今天就來學習一下Java中的隊列。本文的例子使用的Java8環境。
繼承類圖
學習隊列,首先要知道它的類繼承體系,知道每種隊列都實現了哪些介面,繼承了哪些類,這樣有助於協助我們理解。下面是Java8中隊列的類繼承圖。
從繼承類圖中可以看出,隊列主要分兩種,一種是非阻塞隊列,實現了Queue介面,包括LinkedList,ArrayDeque和PriorityQueue;一種是阻塞隊列,實現了BlockingQueue,包括ArrayBlockingQueue,LinkedBlockingQueue,SynchronousQueue和LinkedBlockingDeque.
Iterable介面
實現了Iterable介面的類就具有了迭代的功能,可以被用於“for-Each”迴圈語句。該介面具有的方法如下:
public interface Iterable<T> { Iterator<T> iterator(); default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } default Spliterator<T> spliterator() { return Spliterators.spliteratorUnknownSize(iterator(), 0); }}
Queue介面
Queue介面代表著隊列,在隊頭刪除元素,隊尾插入元素,有著先進先出的特性,下面是介面聲明的方法。
1 public interface Queue<E> extends Collection<E> { 2 3 //插入元素到隊尾,成功返回true,失敗返回false,空間不足拋出異常 4 boolean add(E e); 5 6 //插入元素到隊尾,成功返回true,失敗返回false 7 boolean offer(E e); 8 9 //移除並返回隊頭元素,如果隊列為空白,拋出異常10 E remove();11 12 //移除並返回隊頭元素,如果隊列為空白,則返回null13 E poll();14 15 //返回隊頭元素,如果隊列為空白,則拋出異常16 E element();17 18 //返回隊頭元素,如果隊列為空白,則返回null19 E peek();20 }
Deque介面
Deque是雙端隊列,是Double End Queue的簡稱,與Queue只能在隊頭刪除元素,隊尾插入元素不同,Deque可以在隊列的頭尾分別進行插入和刪除元素。由於可以在同一端進行插入和
刪除元素,因此可以被當做棧來使用。下面是該介面聲明的方法:
public interface Deque<E> extends Queue<E> { /** * 在隊頭插入元素,如果隊列對容量有限制,則容量不足時拋出異常 * IllegalStatException */ void addFirst(E e); /** * 在隊尾插入元素,如果隊列對容量有限制,則容量不足時拋出異常 * IllegalStatException */ void addLast(E e); /** * 在隊頭插入元素,插入成功返回true,失敗返回false * 如果隊列對容量有限制,則最好使用該方法 */ boolean offerFirst(E e); /** * 在隊尾插入元素,成功返回true,失敗返回false; * 如果隊列對容量有限制,則最好使用該方法 */ boolean offerLast(E e); /** * 返回並移除隊頭的元素,如果隊列為空白,則拋出異常 */ E removeFirst(); /** * 返回並移除隊尾的元素,如果隊列為空白,則拋出異常 */ E removeLast(); /** * 返回並移除隊頭的元素,如果隊列為空白,則返回null */ E pollFirst(); /** * 返回並移除隊尾的元素,如果隊列為空白,則返回null */ E pollLast(); /** * 返回隊頭的元素,如果隊列為空白,則拋出異常NoSuchElementException */ E getFirst(); /** * 返回隊尾的元素,如果隊列為空白,則拋出異常NoSuchElementException */ E getLast(); /** * 返回隊頭的元素,如果隊列為空白,則返回null */ E peekFirst(); /** * 返回隊尾的元素,如果隊列為空白,則返回null */ E peekLast(); boolean removeFirstOccurrence(Object o); boolean removeLastOccurrence(Object o); // *** Queue methods *** boolean add(E e); boolean offer(E e); E remove(); E poll(); E element(); E peek(); // *** Stack methods *** void push(E e); E pop(); // *** Collection methods *** boolean remove(Object o); boolean contains(Object o); public int size(); Iterator<E> iterator(); Iterator<E> descendingIterator();}
BlockingQueue介面
BlockingQueue是java.util.concurrent包提供的介面,表示阻塞隊列,與普通隊列的區別是:當從隊頭擷取元素時,如果隊列為空白,則阻塞隊列會阻塞,直到有可用元素、等待逾時或者被中斷;
當需要在隊尾插入元素時,如果隊列沒有可用的空間,則操作會阻塞,直到有可用空間、等待逾時或者被中斷。下面是該介面聲明的方法:
public interface BlockingQueue<E> extends Queue<E> { boolean add(E e); boolean offer(E e); /** * 插入元素,如果空間不足,將會阻塞,直到有可用空間 */ void put(E e) throws InterruptedException; /** * 插入元素,如果容量不足,將會阻塞,直到有可用空間,或者等待逾時 */ boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException; /** * 返回並移除隊頭的元素,如果隊列為空白,則等待 * */ E take() throws InterruptedException; /** * 返回並移除隊頭的元素,如果隊列為空白,則阻塞,直到有可用元素,或者等待逾時 * */ E poll(long timeout, TimeUnit unit) throws InterruptedException; /** * 返回隊列還能存放多少個元素 * 該方法不會被阻塞,直接返回 */ int remainingCapacity(); /** * 刪除給定的元素,如果給定的元素在隊列存在多個,則只刪除第一個 * 成功刪除,返回true,否則,返回false */ boolean remove(Object o); /** * 隊列中是否存在給定的元素,存在返回true,否則返回false */ public boolean contains(Object o); /** * 從隊列中刪除所有元素,並添加到給定的容器c中 * 該方法比迴圈調用poll方法更高效 */ int drainTo(Collection<? super E> c); /** * 最多從隊列中刪除maxElements個元素,並添加到容器c中 */ int drainTo(Collection<? super E> c, int maxElements);}
BlockingDeque介面
BlockingDeque是雙端阻塞隊列,可以在隊列的頭和尾分別進行元素的插入和刪除,可以用作阻塞棧,下面是該介面聲明的方法:
public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { void addFirst(E e); void addLast(E e); boolean offerFirst(E e); boolean offerLast(E e); void putFirst(E e) throws InterruptedException; void putLast(E e) throws InterruptedException; boolean offerFirst(E e, long timeout, TimeUnit unit) throws InterruptedException; boolean offerLast(E e, long timeout, TimeUnit unit) throws InterruptedException; E takeFirst() throws InterruptedException; E takeLast() throws InterruptedException; E pollFirst(long timeout, TimeUnit unit) throws InterruptedException; E pollLast(long timeout, TimeUnit unit) throws InterruptedException; boolean removeFirstOccurrence(Object o); boolean removeLastOccurrence(Object o); // *** BlockingQueue methods *** boolean add(E e); boolean offer(E e); void put(E e) throws InterruptedException; boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException; E remove(); E poll(); E take() throws InterruptedException; E poll(long timeout, TimeUnit unit) throws InterruptedException; E element(); E peek(); boolean remove(Object o); public boolean contains(Object o); public int size(); Iterator<E> iterator(); // *** Stack methods *** void push(E e);}
Java隊列學習