【集合类】源码解析之 Queue接口、AbstractQueue抽象类、Deque接口

Queue接口

队列,除了基本的Collection操作之外,队列还提供额外的插入,提取和检查操作。 这些方法中的每一种都有两种形式:如果操作失败,则抛出一个异常,另一种形式返回一个特殊值( null或false ,具体取决于操作)。

通常(但不一定)是以FIFO(先进先出)方式排序元素
例外情况包括优先级队列(根据提供的比较器对元素进行排序)和LIFO队列(或堆栈)(后进先出)

类声明

public interface Queue<E> extends Collection<E> 

共性方法

/**
 * 将指定元素插入到此队列中
 * IllegalStateException - 如果由于容量限制,此时无法添加该元素 
 * ClassCastException - 如果指定元素的类阻止将其添加到此队列中 
 * NullPointerException - 如果指定的元素为空,并且该队列不允许空元素 
 * IllegalArgumentException - 如果此元素的某些属性阻止将其添加到此队列 
 */
boolean add(E e);

/**
 * 将指定元素插入到此队列中,如果超出队列界限则返回false
 * ClassCastException - 如果指定元素的类阻止将其添加到此队列中
 * NullPointerException - 如果指定的元素为空,并且该队列不允许空元素
 * IllegalArgumentException - 如果此元素的某些属性阻止将其添加到此队列
 */
boolean offer(E e);

/**
 * 检索并删除此队列的头
 * NoSuchElementException - 如果这个队列是空的 
 */
E remove();

/**
 * 检索并删除此队列的头,如果此队列为空,则返回 null 
 */
E poll();

/**
 * 检索但不删除这个队列的头部
 * NoSuchElementException - 如果这个队列是空的 
 */
E element();

/**
 * 检索但不删除此队列的头部,如果此队列为空,则返回 null
 */
E peek();

AbstractQueue抽象类

类声明

public abstract class AbstractQueue<E> extends AbstractCollection<E> implements Queue<E>

方法实现

// 唯一的构造器 给子类初始化使用
protected AbstractQueue() {
}

// 调用offer
public boolean add(E e) {
    if (offer(e))
        return true;
    else
        throw new IllegalStateException("Queue full");
}

// 调用poll
public E remove() {
    E x = poll();
    if (x != null)
        return x;
    else
        throw new NoSuchElementException();
}

// 调用peek
public E element() {
    E x = peek();
    if (x != null)
        return x;
    else
        throw new NoSuchElementException();
}

// 循环调用poll直至返回null
public void clear() {
    while (poll() != null)
        ;
}

// 添加集合中的所有元素近队列,如果在添加元素中遇到运行时异常会导致抛出异常时已成功添加了一部份
public boolean addAll(Collection<? extends E> c) {
    if (c == null)
        throw new NullPointerException();
    if (c == this)
        throw new IllegalArgumentException();
    boolean modified = false;
    for (E e : c)
        if (add(e))
            modified = true;
    return modified;
}

Deque接口

双端队列 (double ended queue),支持在两端插入和移除元素

此接口支持容量受限的deque以及没有固定大小限制的deque,大多数deque的实现对元素的数量没有固定限制

类声明

public interface Deque<E> extends Queue<E>

Queue 方法

boolean add(E e);

boolean offer(E e);

E remove();

E poll();

E element();

E peek();

相似行为

当deque用作队列时,FIFO(先进先出)行为将产生。元素添加在deque的末尾,并从开头删除

void addLast(E e); // 等价于 boolean add(E e)

boolean offerLast(E e); // 等价于 boolean offer(E e)

E removeFirst(); // 等价于 E remove()

E pollFirst(); // 等价于 E poll()

E getFirst(); // 等价于 E element()

E peekFirst(); // 等价于 E peek()

Stack 方法

/**
 * 将元素推送到由此deque表示的堆栈(换句话说,在此deque的头部)
 * IllegalStateException - 如果由于容量限制,此时无法添加该元素
 * ClassCastException - 如果指定元素的类阻止将其添加到此deque
 * NullPointerException - 如果指定的元素为空,并且此deque不允许空元素
 * IllegalArgumentException - 如果指定元素的某些属性阻止将其添加到此deque
 */
void push(E e);

/**
 * 从这个deque表示的堆栈中弹出一个元素(在此deque的头部)
 * NoSuchElementException - 如果这个deque是空的 
 */
E pop();

相似行为

当队列用作的堆栈时,LIFO(后进先出)行为将产生。元素从deque的开头被推入和弹出

void addFirst(E e); // 等价于 void push(E e);

E removeFirst(); // 等价于 E pop();

E peekFirst(); // 等价于 E peek()

其他方法

// 队头插入元素,若不成功返null
boolean offerFirst(E e);

// 队尾删除并返回元素,若队列为空则抛异常
E removeLast();

// 队尾删除并返回元素,若队列为空则返null
E pollLast();

// 检索队尾元素,若队列为空则抛异常
E getLast();

//  检索队尾元素,若队列为空则返null
E peekLast();

// 从此deque中删除指定元素的第一个匹配项
boolean removeFirstOccurrence(Object o);

// 从此deque中删除指定元素的最后一个匹配项
boolean removeLastOccurrence(Object o);

// *** Collection methods ***

boolean remove(Object o);

boolean contains(Object o);

public int size();

Iterator<E> iterator();

// 以相反的顺序返回deque中的迭代器
Iterator<E> descendingIterator();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章