Queue
Queue是队列接口,它的主要特点是先进先出,就是排队嘛。
Queue接口有两个重要的子接口Deque和BlockingQueue,这里只介绍Queue接口的直接是实现,Deque和BlockingQueue接口的实现单独介绍。
public interface Queue<E> extends Collection<E> {
boolean add(E e);
boolean offer(E e);
E remove();
E poll();
E element();
E peek();
}
方法 | 说明 |
---|---|
add | 插入元素,添加成功时返回true,队列满时抛出IllegalStateException异常 |
offer | 插入元素,添加成功时返回true,队列满时返回false |
remove | 删除头元素,队列为空时抛出NoSuchElementException |
poll | 删除头元素,队列为空时返回null |
element | 检索,但不删除此队列的头,队列为空时抛出NoSuchElementException |
peek | 检索,但不删除队列头,队列为空时返回null |
Queue接口的两个重要实现:
- LinkedList:LinkedList源码分析
- PriorityQueue:PriorityQueue源码分析