美文网首页
队列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方法,操作的时候会抛出异常。

    相关文章

      网友评论

          本文标题:队列Queue

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