• Deque


    A linear collection that supports element insertion and removal at both ends. The name deque is short for "double ended queue" and is usually pronounced "deck". Most Deque implementations place no fixed limits on the number of elements they may contain, but this interface supports capacity-restricted deques as well as those with no fixed size limit.

    This interface defines methods to access the elements at both ends of the deque. Methods are provided to insert, remove, and examine the element. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation). The latter form of the insert operation is designed specifically for use with capacity-restricted Deque implementations; in most implementations, insert operations cannot fail.

    The twelve methods described above are summarized in the following table:

    Summary of Deque methods
      First Element (Head) Last Element (Tail)
      Throws exception Special value Throws exception Special value
    Insert addFirst(e)  return void

    /**
    * Inserts the specified element at the front of this deque if it is
    * possible to do so immediately without violating capacity restrictions,
    * throwing an {@code IllegalStateException} if no space is currently
    * available**/

    offerFirst(e) return boolean

    /**
    * Inserts the specified element at the front of this deque unless it would
    * violate capacity restrictions. When using a capacity-restricted deque,
    * this method is generally preferable to the {@link #addFirst} method,
    * which can fail to insert an element only by throwing an exception.
    */

    addLast(e) void offerLast(e)
    Remove removeFirst()

    /**
    * Retrieves and removes the first element of this deque. This method
    * differs from {@link #pollFirst pollFirst} only in that it throws an
    * exception if this deque is empty.
    */


    pollFirst()
    Retrieves and removes the first element of this deque,

    or returns {@code null} if this deque is empty.


    E removeLast()

    Retrieves and removes the last element of this deque.
    This method* differs from {@link #pollLast pollLast}
    only in that it throws an exception if this deque is empty.


    pollLast()
    Examine getFirst()

    /**
    * Retrieves, but does not remove, the first element of this deque.
    *
    * This method differs from {@link #peekFirst peekFirst} only in that it
    * throws an exception if this deque is empty.
    */


    peekFirst()

    /**
    * Retrieves, but does not remove, the first element of this deque,
    * or returns {@code null} if this deque is empty.
    */


    getLast() peekLast()

    This interface extends the Queue interface. When a deque is used as a queue, FIFO (First-In-First-Out) behavior results. Elements are added at the end of the deque and removed from the beginning. The methods inherited from the Queue interface are precisely equivalent to Deque methods as indicated in the following table:

    Comparison of Queue and Deque methods
    Queue Method Equivalent Deque Method
    add(e) addLast(e)
    offer(e) offerLast(e)
    remove() removeFirst()
    poll() pollFirst()
    element() getFirst()
    peek() peekFirst()

    Deques can also be used as LIFO (Last-In-First-Out) stacks. This interface should be used in preference to the legacy Stack class. When a deque is used as a stack, elements are pushed and popped from the beginning of the deque. Stack methods are precisely equivalent to Deque methods as indicated in the table below:

    Comparison of Stack and Deque methods
    Stack Method Equivalent Deque Method
    push(e) addFirst(e)
    pop() removeFirst()
    peek() peekFirst()

    Note that the peek method works equally well when a deque is used as a queue or a stack; in either case, elements are drawn from the beginning of the deque.

    This interface provides two methods to remove interior elements, removeFirstOccurrence and removeLastOccurrence.

    Unlike the List interface, this interface does not provide support for indexed access to elements.

    While Deque implementations are not strictly required to prohibit the insertion of null elements, they are strongly encouraged to do so. Users of any Deque implementations that do allow null elements are strongly encouraged not to take advantage of the ability to insert nulls. This is so because null is used as a special return value by various methods to indicated that the deque is empty.

    Deque implementations generally do not define element-based versions of the equals and hashCode methods, but instead inherit the identity-based versions from class Object.

    This interface is a member of the Java Collections Framework.

  • 相关阅读:
    代码规范问题
    HTML页面中显示HTML标签<xmp>
    controller.tabBarItem.title = @"11111"不显示
    xcode9报错 Safe Area Layout Guide before iOS9.0
    iBeacon
    protocol buffer
    关于沙漠君 | 一只特立独行的猪
    机器学习预测机动车摇号:神秘的第七位
    专注的价值
    Hawk 3.1 动态页面,ajax,瀑布流
  • 原文地址:https://www.cnblogs.com/Ivyduan/p/15323306.html
Copyright © 2020-2023  润新知