Java中Queue接口与List、Set同一级别,都是继承了Collection接口。是一种常用的数据结构。其实现是由LinkedList实现。
Queue是一种有序处理数据的的集合,包含Collection的所有基本操作,还提供另外的插入、提取和检查操作。这几种方法都存在两种形式:一种如果操作失败则抛出异常,另一种则返回一个特殊值(null或false)。后者的插入操作是专门为有容量限制的队列实现的,在大多数实现中,插入操作不能失败。
Queue的方法总结:
throw exception | return specific value | |
Insert | add(e) | offer(e) |
Remove | remove() | poll() |
Examine | element() | peek() |
Queue通常但未必全是以FIFO的方式排列元素,priority queue就是一个例外,它根据提供的comparator来存储元素,或者元素的本身顺序,LIFO queue也就是stack,以后进先出的方式处理元素。无论使用哪种顺序,queue的head都是调用remove或poll方法将要移除的那个元素。在FIFO队列中,所有新的元素都插入在队尾,其他的队列则可能采用不同的放置规则。每个Queue的实现都要明确它的处理顺序。
offer方法尽可能插入一个元素,否则返回false,与继承自Collection的add方法不同,add方法操作失败则会抛出一个unchecked exception。offer方法使用在当失败是常见的情况下,而不是异常情况,例如,在一个有容量限制的队列中。
remove和poll方法删除队首元素并返回该元素。这里指出,删除哪一个元素是决定于队列的存储元素的顺序,不同的实现删除的元素会不同。remove和poll方法只在当队列为空的时候行为不同:remove方法抛出异常,而poll方法返回null。
element和peek方法都返回但不删除队首元素。
Queue接口并没有定义阻塞队列的方法(blocking queue methods),blocking queue在如今的并发编程中很常见,其定义是在java.util.concurrent.BlockingQueue接口,继承Queue接口。
Queue实现通常不允许插入null元素,尽管像LinkedList等实现并没有禁止插入null。甚至在允许插入null的实现中,null也不能插入队列,因为null被用作poll方法的特定返回值,表明队列为空。
Queue没有定义基于元素的equals和hashCode方法,而是直接继承自Object类,因为基于元素的相等对于队列来说不太好定义,当队列中有相同的元素但是排在不同的顺序。
源码中的接口定义:
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(); }
Tip: 插入和删除通常使用offer和poll方法,而不使用继承自Collection的add和remove方法,因为失败时其特定的返回值,可以判断队列此时的状态。