`
cywhoyi
  • 浏览: 418224 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

介绍些不主流的Collection的工具

 
阅读更多

Common Definitions

First, let’s start with some definitions. In common usage of the word, a queue is FIFO (first-in-first-out). Just like the first person in line at the post office gets served first. A stack is LIFO (last in first out). Think of a stack of books – the last book you put on the stack is the first book you take off.

Choices in Java

In Java, things are a little more complicated, but the same principles apply. There is the aforementioned Queue interface which has the expected methods to add, remove and look at elements. (Actually, those methods come in two flavors: one throws an exception if the operation fails, the other returns a special value such as null or false; see more here).

There is also a sub-interface of Queue called Deque, short for “double ended queue” and is usually pronounced “deck”. This interface defines methods to access the elements at both ends of the collection. This functionality makes Deque a useful base for both queue and stack implementations.

However, both Queue and Deque are interfaces, so we still haven’t found a concrete implementation to use yet…

The shortlist: ArrayDeque & LinkedList

The 2 main choices are: ArrayDeque and LinkedList. There are a couple of other implementation of Deque such as ConcurrentLinkedDeque and LinkedBlockingDeque, and a plethora of implementations of Queue such as DelayQueue, LinkedBlockingDeque and PriorityQueue. Since those are more specialized (and I haven’t used them much), I won’t go into here.

ArrayDeque

From the javadocs, ArrayDeque is a resizable-array implementation of the Deque interface. Most ArrayDeque operations run in amortized constant time (i.e. slower “once in a while”, but on average constant) except for remove*, contains* and the bulk operations, all of which run in linear time. The docs point out that this class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue. It is this statement that leads me to use ArrayDeque as my default implmentation for both stacks and queues.

LinkedList

The LinkedList class implements the List, Queue and Deque interfaces. In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue.

LinkedList vs ArrayDeque

So, when would you use a LinkedList over an ArrayDeque?

Pros of a LinkedList implementation are:

  • more flexible than the ArrayDeque implementation, as it
    • implements all optional list operations.
    • allows null elements (not allowed in ArrayDeque)
  • well suited when you need to remove/insert items in the middle of the list frequently (something that can result in large array copies for the ArrayDeque).

Cons of a LinkedList implementation:

  • not ideal when iterating over items in general
  • consumes more memory than the ArrayDeque implementation

Overall

In terms of efficiency, ArrayDeque is more efficient than the LinkedList for iteration and add/remove operation at both ends. So, as the javadocs point out, in general, ArrayDeque is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue.

        

分享到:
评论

相关推荐

    汇编语言编写工具(个人收集的四个工具)

    以下是对标题和描述中提及的四个个人收集的汇编语言编写工具的详细介绍: 1. **NASM (Netwide Assembler)** NASM是一个流行的开源汇编器,支持x86和x86-64架构。它的语法简洁,易于理解,并且适用于多种操作系统,...

    C++代码编译工具软件

    本话题将详细介绍C++代码编译工具软件及其重要性。 首先,让我们关注“cmake-3.24.2-windows-x86_64.msi”和“cmake-3.24.2-windows-x86_64.zip”这两个文件。它们都是与CMake相关的资源,CMake是一个开源的跨平台...

    Java-Interview-Collection.rar_java 面试

    1. **基础知识**:面试通常会从Java语言的基础开始,包括但不限于数据类型、运算符、流程控制语句(如if,switch,for,while)、数组、字符串处理、异常处理等。 2. **面向对象编程**:理解类、对象、继承、多态、...

    gnugccmake等底层开发工具dos版本的

    标题提到的"gnugccmake等底层开发工具dos版本的",指的是在DOS(Disk Operating System)操作系统环境下使用的GNU GCC(GNU Compiler Collection)、Make等工具的DOS适配版本。 **GNU GCC** 是一个开源的编译器套件...

    portfolio-collection:练习不同的工具以实现相似的目标

    【标题】"portfolio-collection:练习不同的工具以实现相似的目标"主要体现了在IT行业中,开发者经常需要使用各种不同的工具和技术来达成相同或相似的项目目标。这通常涉及到技术选型、工具整合以及对不同平台和语言...

    java 主流面试题

    - **Collections**:工具类,提供了一系列静态方法来操作集合对象。 #### 11. HashMap与Hashtable - **HashMap**:允许键和值为null,线程不安全。 - **Hashtable**:不允许键或值为null,线程安全。 #### 12. ...

    jdk各版本包下载 含主流1.5、1.6、1.8版本

    本资源提供了三个主流的JDK版本,分别是1.5、1.6和1.8,它们在Java发展历程中占据了重要位置。 1. **JDK 1.5(也称为Java 5.0)** JDK 1.5于2004年发布,引入了许多重要的新特性,包括: - **泛型(Generics)**...

    Method-collection-of-Java-encryption.zip_DSA java

    本文将深入探讨标题"Method-collection-of-Java-encryption.zip_DSA java"中涉及的几种主流加密算法在Java中的应用,包括DES、3DES、AES、RSA、DSA、MD5和SHA1。 1. **DES(Data Encryption Standard)**:DES是一...

    主流ARM嵌入式系统设计技术与实例精粹 配套源码

    开发ARM嵌入式系统通常需要使用交叉编译器,如GCC(GNU Compiler Collection),以及调试工具链,如GDB(GNU Debugger)。此外,IDE(Integrated Development Environment)如Keil、IAR、Eclipse等可以提供便捷的...

    TurboC2.0 C语言开发工具

    TurboC2.0是 Borland 公司在1980年代末至1990年代初推出的一款经典的C语言集成开发环境(IDE),它在当时的个人计算机领域中扮演了重要角色,尤其对于初学者来说,是学习C语言的首选工具。这个软件包包含了编译器、...

    ARM.rar_ARM介绍1

    对于ARM开发,通常需要交叉编译工具链,如GCC(GNU Compiler Collection)和GNU Make。开发环境可能包括Eclipse、Keil MDK或GNU的ARM Eclipse插件。这些工具帮助开发者将源代码编译为可在ARM处理器上运行的目标代码...

    github-FE-project, A collection about github front-end project.zip

    这个文件可能包含了每个项目的README文件,提供了项目的基本介绍、安装指南、使用方法以及贡献方式等信息。此外,还可能包含源码文件、资源文件、测试文件等,为开发者提供了完整的项目结构。 从这个压缩包中,我们...

    Yoogli: Add URL To Collection-crx插件

    值得注意的是,Yoogli: Add URL To Collection-crx作为一款浏览器扩展,可能支持Chrome、Firefox等主流浏览器。安装过程通常非常简单,只需将Yoogli__Add_URL_To_Collection.crx文件拖放到浏览器的扩展管理页面即可...

    Linux 开发的一些基本介绍和指南

    ### Linux开发基本介绍与指南 Linux作为开源操作系统中的佼佼者,在全球范围内有着广泛的用户群体。无论是个人开发者还是企业级应用,Linux都以其强大的稳定性、安全性以及灵活性成为了首选的开发平台之一。对于...

    flex测试总结

    例如,"FIL_iPad_GUI_Collection.png"和"FIL_iPhone_GUI_1_0.png"可能是iOS设备上的界面截图,用于比对设计与实现的差异。"BulkBuyDesign20130812.pptx"可能是一个产品购买界面的设计稿,测试人员需要确保实际开发的...

    Addison-Wesley Professional Cross-Platform Development in C++.2008

    跨平台开发需要深入了解不同平台上的编译器和工具链,如GCC(GNU Compiler Collection)在Linux和Mac OS X中的使用,以及Microsoft Visual C++在Windows平台的应用。本书提供了详尽的指南,帮助读者选择合适的编译器...

    android+eclipse+ndk环境搭配

    Cygwin包含了众多开发工具,如GCC(GNU Compiler Collection)和GDB(GNU Debugger),并且支持X Window System,方便图形界面的开发。 **Cygwin的特性与限制** Cygwin的主要优点在于它的库可以在Win32系统下实现...

Global site tag (gtag.js) - Google Analytics