Queue
version:1.8
不同于Stack类提供的栈的具体实现,Queue为JDK提供的队列(先进先出)的接口。
/**
* @see java.util.Collection
* @see LinkedList
* @see PriorityQueue
* @see java.util.concurrent.LinkedBlockingQueue
* @see java.util.concurrent.BlockingQueue
* @see java.util.concurrent.ArrayBlockingQueue
* @see java.util.concurrent.LinkedBlockingQueue
* @see java.util.concurrent.PriorityBlockingQueue
* **/
public interface Queue<E> extends Collection<E>
其定义了六个方法,分为两类,每类三个(进,出,查看队首元素),两类方法的区别主要在于对队列为空(添加失败) 时的处理方法。
第一类:队列为空时返回null
添加失败(非添加元素原因)返回false
队列为空返回null
/**
* @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);
//@return the head of this queue, or {@code null} if this queue is empty
E poll();
//@return the head of this queue, or {@code null} if this queue is empty
E peek();
第二类:队列为空抛出异常
若由于容量限制添加失败抛出异常IllegalStateException
队列为空抛出异常NoSuchElementException
/**
* @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);
//@return the head of this queue
//@throws NoSuchElementException if this queue is empty
E remove();
//@return the head of this queue
//@throws NoSuchElementException if this queue is empty
E element();