美文网首页
Java 集合框架(Deque 接口)

Java 集合框架(Deque 接口)

作者: 6ea566508d0d | 来源:发表于2018-11-25 19:21 被阅读0次

    Deque 接口简介

    对于 Deque 接口,Java的官方文档这样提到:

    Usually pronounced as deck, a deque is a double-ended-queue. A double-ended-queue is a linear collection of elements that supports the insertion and removal of elements at both end points. The Deque interface is a richer abstract data type than both Stack and Queue because it implements both stacks and queues at the same time. The Deque interface, defines methods to access the elements at both ends of the Deque instance. Methods are provided to insert, remove, and examine the elements. Predefined classes like ArrayDeque and LinkedList implement the Deque interface.
    Note that the Deque interface can be used both as last-in-first-out stacks and first-in-first-out queues.

    简单点来说就是:

    • Deque 是双端队列,支持从两端插入、删除、检查元素
    • Deque 是比 StackDeque 更丰富的数据类型
    • Deque 同时实现了堆栈和队列,既可用作后进先出堆栈,也可用作先进先出队列

    Deque 接口相关操作

    Deque 接口的方法和 Queue 接口类似,每种方法也有两种形式:

    Type of Operation First Element (Beginning of the Deque instance) Last Element (End of the Deque instance)
    Insert addFirst(e)
    offerFirst(e)
    addLast(e)
    offerLast(e)
    Remove removeFirst()
    pollFirst()
    removeLast()
    pollLast()
    Examine getFirst()
    peekFirst()
    getLast()
    peekLast()

    这里也不在多累述,大概就是这样:

    • add()remove()get() 操作失败,抛出异常
    • offer()poll()peek() 操作失败,返回一个特殊的值(nullfalse)

    相关文章

      网友评论

          本文标题:Java 集合框架(Deque 接口)

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