`
Donald_Draper
  • 浏览: 985012 次
社区版块
存档分类
最新评论

Queue接口定义

    博客分类:
  • JUC
阅读更多
/*
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * Written by Doug Lea with assistance from members of JCP JSR-166
 * Expert Group and released to the public domain, as explained at
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

package java.util;

/**
 * A collection designed for holding elements prior to processing.
 * Besides basic {@link java.util.Collection Collection} operations,
 * queues provide additional insertion, extraction, and inspection
 * operations.  Each of these methods exists in two forms: one throws
 * an exception if the operation fails, the other returns a special
 * value (either <tt>null</tt> or <tt>false</tt>, depending on the
 * operation).  The latter form of the insert operation is designed
 * specifically for use with capacity-restricted <tt>Queue</tt>
 * implementations; in most implementations, insert operations cannot
 * fail.
 *
Queue优先处理元素,除了Collection的基本操作之外,Queue添加了另外插入,拉取,
检查3个操作。每一种方法都有两种格式,一种操作失败,则抛出异常,另一种返回一个
特殊的值,null或false,依赖于操作。后一种形式的插入操作是用capacity-restricted
Queue来实现的,在大多数的实现中,插入操作不会失败。
 * <p>
 * <table BORDER CELLPADDING=3 CELLSPACING=1>
 *  <tr>
 *    <td></td>
 *    <td ALIGN=CENTER>[i]Throws exception[/i]</td>
 *    <td ALIGN=CENTER>[i]Returns special value[/i]</td>
 *  </tr>
 *  <tr> 插入,提供
 *    <td><b>Insert</b></td>
 *    <td>{@link #add add(e)}</td>
 *    <td>{@link #offer offer(e)}</td>
 *  </tr>
 *  <tr>移除,拉取
 *    <td><b>Remove</b></td>
 *    <td>{@link #remove remove()}</td>
 *    <td>{@link #poll poll()}</td>
 *  </tr>
 *  <tr>检查
 *    <td><b>Examine</b></td>
 *    <td>{@link #element element()}</td>
 *    <td>{@link #peek peek()}</td>
 *  </tr>
 * </table>
 *
 * <p>Queues typically, but do not necessarily, order elements in a
 * FIFO (first-in-first-out) manner.  Among the exceptions are
 * priority queues, which order elements according to a supplied
 * comparator, or the elements' natural ordering, and LIFO queues (or
 * stacks) which order the elements LIFO (last-in-first-out).
 * Whatever the ordering used, the [i]head[/i] of the queue is that
 * element which would be removed by a call to {@link #remove() } or
 * {@link #poll()}.  In a FIFO queue, all new elements are inserted at
 * the [i] tail[/i] of the queue. Other kinds of queues may use
 * different placement rules.  Every <tt>Queue</tt> implementation
 * must specify its ordering properties.
 *
Queues除了非必要之外,元素的模式为FIFO。其中的一个例外是优先级队列,元素根据
比较器或者本身的顺序添加队列( LIFO queues)中。无论什么顺序,对头的元素将会
被#remove或#poll消费/移除。在先进先出队列中,所有的新元素插入到队尾,其他队列
可能会用其他的策略添加新元素。每一种队列的实现要明确排序策略。

 * <p>The {@link #offer offer} method inserts an element if possible,
 * otherwise returning <tt>false</tt>.  This differs from the {@link
 * java.util.Collection#add Collection.add} method, which can fail to
 * add an element only by throwing an unchecked exception.  The
 * <tt>offer</tt> method is designed for use when failure is a normal,
 * rather than exceptional occurrence, for example, in fixed-capacity
 * (or "bounded") queues.
 *
 如果可能,offer操作,插入元素时,会返回false。这个与集合类的add方法不同的时,
 集合类add方法,当失败时,则抛出一个unchecked异常。offer操作设计为在一个正常模式下的失败,
 而不异常发生,比如队列容量有限。
 * <p>The {@link #remove()} and {@link #poll()} methods remove and
 * return the head of the queue.
 * Exactly which element is removed from the queue is a
 * function of the queue's ordering policy, which differs from
 * implementation to implementation. The <tt>remove()</tt> and
 * <tt>poll()</tt> methods differ only in their behavior when the
 * queue is empty: the <tt>remove()</tt> method throws an exception,
 * while the <tt>poll()</tt> method returns <tt>null</tt>.
 *
 * <p>The {@link #element()} and {@link #peek()} methods return, but do
 * not remove, the head of the queue.
 *
remove和poll元素将从队列头部消费一个元素。不同的队列实现消费队列元素顺序时,可能有不同的
策略。remove和poll操作不同的时,当队列为空时,remove抛出异常 ,poll则返回null。

 * <p>The <tt>Queue</tt> interface does not define the <i>blocking queue
 * methods</i>, which are common in concurrent programming.  These methods,
 * which wait for elements to appear or for space to become available, are
 * defined in the {@link java.util.concurrent.BlockingQueue} interface, which
 * extends this interface.
 *
Queue没有定义阻塞队列的方法,这些方法会在并发编程中用到。这些方法,比如,在队列为空时,
消费者阻塞等待队列有元素可利用,这些方法在BlockingQueue接口中定义。
 * <p><tt>Queue</tt> implementations generally do not allow insertion
 * of <tt>null</tt> elements, although some implementations, such as
 * {@link LinkedList}, do not prohibit insertion of <tt>null</tt>.
 * Even in the implementations that permit it, <tt>null</tt> should
 * not be inserted into a <tt>Queue</tt>, as <tt>null</tt> is also
 * used as a special return value by the <tt>poll</tt> method to
 * indicate that the queue contains no elements.
 队列的实现通常不允许插入null,尽管LinkedList不禁止插入null值。即使允许插入null,
 我们也利用利用poll操作,来暗示队列中没有元素。
 *
 * <p><tt>Queue</tt> implementations generally do not define
 * element-based versions of methods <tt>equals</tt> and
 * <tt>hashCode</tt> but instead inherit the identity based versions
 * from class <tt>Object</tt>, because element-based equality is not
 * always well-defined for queues with the same elements but different
 * ordering properties.
 *
 Queue的实现通常不会定义equals和hashCode方法,而是继承自Object,因为hash值和值相等
 ,但是顺序属性可能不同。
 *
 * <p>This interface is a member of the
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
 * Java Collections Framework</a>.
 *
 * @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
 * @since 1.5
 * @author Doug Lea
 * @param <E> the type of elements held in this collection
 */
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
     * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
     * if no space is currently available.
     *
     在有界队列未满的情况下,添加元素,返回true,没空间可利用,则抛出异常IllegalStateException
     * @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
     */
    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.
     *
      在有界队列未满的情况下,添加元素,立即成功;当队列为有界是,add更好,因为越界抛出异常
     * @param e the element to add
     * @return <tt>true</tt> if the element was added to this queue, else
     *         <tt>false</tt>
     * @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 <tt>null</tt> if this queue is empty.
     *
     从对头消费一个元素,当队列为空,返回null
     * @return the head of this queue, or <tt>null</tt> 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 <tt>null</tt> if this queue is empty.
     *
      检查队列中是否有元素,当队列为空,返回null
     * @return the head of this queue, or <tt>null</tt> if this queue is empty
     */
    E peek();
}

附:Collection


  • 大小: 15.1 KB
0
0
分享到:
评论

相关推荐

    Java 实例 - 队列(Queue)用法源代码-详细教程.zip

    Queue接口定义了一系列的方法,如`add(E e)`用于在队尾添加元素,`remove()`或`poll()`用于移除并返回队头元素,`element()`或`peek()`用于查看但不移除队头元素。 2. **实现Queue接口的类**:Java提供了多种实现...

    Queue实现类1

    Queue 接口定义了队列操作,如 `offer()`(添加元素)、`poll()`(移除并返回队首元素)、`peek()`(查看但不移除队首元素)等。而 Deque 接口扩展了 Queue,增加了对两端操作的支持,例如 `addFirst()`、`addLast()...

    集合.pdf

    Queue 接口定义了一组用于操作队列的方法,包括 add(E element)、offer(E element)、remove()、poll()、element() 和 peek() 方法。 Deque 接口是 Queue 接口的子接口,代表了一种具有队列和栈特性的数据结构,支持...

    java集合的使用.zip

    5. Queue: Java中的Queue接口定义了队列操作,如add、peek(查看但不移除)、poll(移除并返回首元素)。`Queue_LinkedList.java`展示了如何使用LinkedList实现队列操作。 6. HashSet: 基于哈希表实现的无序、不...

    14.栈和队列.pdf

    Queue接口定义了add、remove、element等方法用于队列操作。Deque接口则扩展了Queue接口,提供了addFirst、addLast、removeFirst、removeLast等方法来支持双端队列的操作。 在使用Java中的栈和队列时,需要理解这些...

    Java源码分析:集合-容器.pdf

    Queue接口定义了先进先出的队列操作,LinkedList实现了Queue接口,因此可以作为一个队列来使用。除了基本的队列操作外,Java还提供了PriorityQueue等具有优先级功能的队列。 在Java集合框架中,各种集合类型的自动...

    Java基础及集合面试宝典(二).pdf

    Queue接口定义了一个队列的数据结构,它的特点是先进先出(FIFO)。Collection接口的实现类包括了诸如ArrayList、LinkedList、HashSet、TreeSet、PriorityQueue等,这些类提供了不同方式存储和访问数据集合的能力。 ...

    java中queue接口的使用详解

    这意味着`Queue`接口需要实现`Collection`接口定义的所有方法,并且具有自己的特定方法。 2. **主要实现类**: - `LinkedList`:这个类实现了`List`接口和`Queue`接口,因此它可以作为队列使用。`LinkedList`内部...

    部分Java数据结构使用

    Queue接口定义了enqueue(add())、dequeue(remove())等方法,而PriorityQueue则提供了一个根据元素自然排序或自定义比较器进行排序的队列。 在实际应用中,理解并熟练使用这些数据结构能帮助开发者编写更高效的...

    java-data-struct.rar_数据结构 java_数据结构源码

    Java.util.Queue接口定义了队列操作,如enqueue(add)和dequeue(remove)。 5. **集合(Set)**:Java.util.Set接口表示不包含重复元素的集合,例如HashSet类提供了快速查找功能。 6. **映射(Map)**:Map接口...

    jdk的数据结构分析1

    5. **Queue**:队列是一种先进先出(FIFO)的数据结构,Java中Queue接口定义了队列操作,如enqueue(入队)和dequeue(出队)。特别地,**BlockingQueue**是线程安全的队列,提供了阻塞操作,当队列为空时,获取元素...

    Dart by Example.pdf

    Queue接口定义了一组用于在集合两端添加和移除元素的方法。 函数是Dart程序的基本构建块。Dart支持顶级函数,也允许在类或对象中定义方法。可选参数可以使得函数调用更加灵活,而命名参数则允许开发者指定参数名称...

    数据结构java

    4. **Queue**:Java中的Queue接口定义了先进先出(FIFO)的数据结构,如ArrayDeque和LinkedList可以作为其实现。PriorityQueue可以按优先级排序元素。 5. **Set**:Set接口表示不包含重复元素的集合,如HashSet和...

    java数据结构源码

    4. **队列**(Queue):队列是一种先进先出(FIFO)的数据结构,Java的java.util.Queue接口定义了队列操作,例如enqueue(add)和dequeue(remove)。队列常用于任务调度、事件处理和广度优先搜索等。 5. **树**...

    data structures with java source code

    Java的java.util.Queue接口定义了队列操作,例如add()用于添加元素,remove()或poll()用于移除队首元素,peek()用于查看队首元素。 5. **队列的变种:优先队列(Priority Queue)**:优先队列按照优先级顺序处理...

    JAVA 数据结构之Queue处理实例代码

    Queue接口定义了许多方法,如`add()`、`offer()`、`remove()`、`poll()`、`peek()`等,用于实现队列的各种操作。 在提供的实例代码中,我们看到了如何使用LinkedList实现一个队列,因为LinkedList类实现了Queue接口...

    thinkphp5.0.24+queue 队列信息完整源码

    创建队列任务,首先在`think`目录下创建一个Job类,如`app\job\SendEmail.php`,定义处理邮件发送的逻辑。然后在控制器中,使用`Queue::push`方法将任务推入队列: ```php namespace app\controller; use think\...

    tp5.1消息队列 think-queue

    4. **创建任务**:定义自己的任务类,继承自 `\think\queue\Job` 接口,实现任务逻辑。 5. **发布任务**:在需要触发任务的地方,调用`Queue::push()`方法,传入任务类名和任务参数。 6. **启动消费者**:运行队列...

    jms-1.1接口定义代码

    在JMS 1.1中,`jms-1.1`可能包含了这些接口的定义,如`javax.jms.Queue`, `javax.jms.Topic`, `javax.jms.MessageProducer`, `javax.jms.MessageConsumer`, `javax.jms.Session`等。通过这些接口,开发者可以构建...

    java 接口练习作业

    Java集合框架包括List、Set、Queue等接口,它们定义了对数据进行操作的一系列规范。例如,`List`接口代表有序的、可重复的元素集合,提供了添加、删除、查找和遍历元素的方法。常见的实现类有ArrayList和LinkedList...

Global site tag (gtag.js) - Google Analytics