美文网首页
队列Queue

队列Queue

作者: Charein | 来源:发表于2019-01-29 14:26 被阅读0次

Queue是一个接口,它本身继承于Collection,而Collection也是一个接口。

1. Queue接口定义

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();
}

当队列已满时插入数据/队列为空时获取数据,总结如下表格:

特殊值 抛出异常
插入 offer add
移除 pool remove
检查结果 peek element

2. AbstractQueue

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

    /**
     * Constructor for use by subclasses.
     */
    protected AbstractQueue() {
    }

    /**
     * Inserts the specified element into this queue if it is possible to do so
     * immediately without violating capacity restrictions, returning
     * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
     * if no space is currently available.
     *
     * <p>This implementation returns <tt>true</tt> if <tt>offer</tt> succeeds,
     * else throws an <tt>IllegalStateException</tt>.
     *
     * @param e the element to add
     * @return <tt>true</tt> (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
     */
    public boolean add(E e) {
        if (offer(e))
            return true;
        else
            throw new IllegalStateException("Queue full");
    }

    /**
     * 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.
     *
     * <p>This implementation returns the result of <tt>poll</tt>
     * unless the queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    public E remove() {
        E x = poll();
        if (x != null)
            return x;
        else
            throw new NoSuchElementException();
    }

    /**
     * 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.
     *
     * <p>This implementation returns the result of <tt>peek</tt>
     * unless the queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    public E element() {
        E x = peek();
        if (x != null)
            return x;
        else
            throw new NoSuchElementException();
    }

对比Queue接口定义,分别实现了add/remove/element方法,操作的时候会抛出异常。

相关文章

  • 循环队列的实现方法1

    设:队列长度是QUEUE_LENGTH队列数组是queue[QUEUE_LENGTH]队列头索引是head队列尾索...

  • Java—Queue队列详解

    Queue Queue队列介绍   Queue是用于模拟队列的,啥叫队列?队列就是排队的意思,比如排队结账,先进入...

  • Java—Queue队列详解(Deque/PriorityQue

    Queue Queue队列介绍   Queue是用于模拟队列的,啥叫队列?队列就是排队的意思,比如排队结账,先进入...

  • Queue模块

    一、class Queue.Queue 类 Queue类表示使用FIFO队列 Queue.qsize()返回队列的...

  • 多线程GCD

    1:GCD 创建队列: 串行队列: dispatch_queue_t queue=dispatch_queue_c...

  • 第三周_总结

    队列创建一个队列:queue_obj = queue.Queue(maxsize=30)maxsize :表示允许...

  • GCD 多线程的使用

    1.串行队列 1.1串行队列创建 dispatch_queue_t queue = dispatch_queue_...

  • GCD

    dispatch_queue_t:线程、队列dispatch_queue_create(派发队列)派发队列分为两种...

  • GCD队列queue.h__queue

    队列queue.h方法总览 创建队列(queue)相关方法: 举例说明:

  • GCD相关

    创建队列 dispatch_queue_create("我是串行队列",DISPATCH_QUEUE_SERIAL...

网友评论

      本文标题:队列Queue

      本文链接:https://www.haomeiwen.com/subject/lprxsqtx.html