ArrayList vs LinkedList vs Vector

From the hierarchy diagram, they all implement List interface. They are very similar to use. Their main difference is their implementation which causes different performance for different operations.

  • ArrayList is implemented as a resizable array. As more elements are added to ArrayList, its size is increased dynamically. It's elements can be accessed directly by using the get and set methods, since ArrayList is essentially an array.

  • LinkedList is implemented as a double linked list. Its performance on add and remove is better than Arraylist, but worse on get and set methods.

  • Vector is similar with ArrayList, but it is synchronized. ArrayList is a better choice if your program is thread-safe. Because of this, it has an overhead than ArrayList. Normally, most Java programmers use ArrayList instead of Vector because they can synchronize explicitly by themselves.

  • Vector and ArrayList require space as more elements are added. Vector each time doubles its array size, while ArrayList grow 50% of its size each time.

  • LinkedList, however, also implements Queue interface which adds more methods than ArrayList and Vector, such as offer(), peek(), poll(), etc.

  • Note: The default initial capacity of an ArrayList is pretty small. It is a good habit to construct the ArrayList with a higher initial capacity. This can avoid the resizing cost.

In brief, LinkedList should be preferred if:

  • there are no large number of random access of element

  • there are a large number of add/remove operations

HashMap vs TreeMap vs LinkedHashMap

All three classes implement the Map interface and offer mostly the same functionality. The most important difference is the order in which iteration through the entries will happen:

  • HashMap makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added.

  • TreeMap is a tree based mapping. Its put/get operations take O(log n) time. It requires items to have some comparison mechanism, either with Comparable or Comparator. TreeMap will iterate according to the "natural ordering" of the keys according to their compareTo() method (or an externally supplied Comparator). It also implements the SortedMap interfacer.  It doesn’t use equals() and hashCode() methods for comparison of elements.

  • LinkedHashMap is very similar to HashMap, but it adds awareness to the order at which items are added (or accessed), so the iteration order is the same as insertion order (or access order, depending on construction parameters)

HashSet Vs TreeSet Vs LinkedHashSet In Java

    • HashSet is Implemented using a hash table. Elements are not ordered. The add, remove, and contains methods have constant time complexity O(1).

    • TreeSet is implemented using a tree structure(red-black tree in algorithm book). The elements in a set are sorted, but the add, remove, and contains methods has time complexity of O(log (n)). It offers several methods to deal with the ordered set like first(), last(), headSet(), tailSet(), etc. It doesn’t use equals() and hashCode() methods for comparision of elements.

    • LinkedHashSet is between HashSet and TreeSet. It is implemented as a hash table with a linked list running through it, so it provides the order of insertion. The time complexity of basic methods is O(1).

Lists, Maps and Sets in Java的更多相关文章

  1. 使用Maps与Sets处理集合的交差运算

    import com.google.common.collect.MapDifference; import com.google.common.collect.Maps; import java.u ...

  2. Spring EL Lists, Maps example

    In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way ...

  3. Java Garbage Collection Basics--转载

    原文地址:http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html Overview Purpose ...

  4. HIBERNATE - 符合Java习惯的关系数据库持久化(精华篇)

    HIBERNATE - 符合Java习惯的关系数据库持久化      下一页 HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.0.4   目录 前言 1. ...

  5. Guava 集合框架

    在本系列中我们首先来学习一些Guava的集合框架,也就是这个package:com.google.common.collect 在这个包下面有一些通用的集合接口和一些相关的类.   集合类型: BiM ...

  6. G1垃圾收集器官方文档透彻解读【官方解读】

    在前几次中已经对G1的理论进行了一个比较详细的了解了,对于G1垃圾收集器最权威的解读肯定得上官网,当咱们将官网的理解透了,那基本上网上对于G1的说明其实最终都是来自于官网,所以接下来会详细来解读Ora ...

  7. Hibernate4教程六:性能提升和二级缓存

    抓取策略(fetching strategy)是指:当应用程序需要在(Hibernate实体对象图的)关联关系间进行导航的时候,Hibernate如何获取关联对象的策略.抓取策略可以在O/R映射的元数 ...

  8. 005-guava 集合-集合工具类-java.util.Collections中未包含的集合工具[Maps,Lists,Sets],Iterables、Multisets、Multimaps、Tables

    一.概述 工具类与特定集合接口的对应关系归纳如下: 集合接口 属于JDK还是Guava 对应的Guava工具类 Collection JDK Collections2:不要和java.util.Col ...

  9. java基础之 GC

    Java程序员在编码过程中通常不需要考虑内存问题,JVM经过高度优化的GC机制大部分情况下都能够很好地处理堆(Heap)的清理问题.以至于许多Java程序员认为,我只需要关心何时创建对象,而回收对象, ...

随机推荐

  1. WPF之路五:wpf 隐藏与显示 Visibility

    WPF里枚举变量Visibility 有三个值:Visible, Collapsed和Hidden.其中Collapsed是WPF新引进的,其作用是不仅隐去Control,同时也会移除Control所 ...

  2. cassandra高级操作之JMX操作

    需求场景 项目中有这么个需求:统计集群中各个节点的数据量存储大小,不是记录数. 一开始有点无头绪,后面查看cassandra官方文档看到Monitoring章节,里面说到:Cassandra中的指标使 ...

  3. IDEA 根据数据库表 生成 Model实体

    首先在 Idea中 配置数据源,测试ok(过程略) idea窗口中左下角是否 有显示Persistence 窗口  如果没有显示就要如下方法 添加显示: File - > Project Str ...

  4. 多云时代,海外微软Azure云与国内阿里云专线打通性能测试

    本文地址:http://www.cnblogs.com/taosha/p/6528730.html 在云计算的大时代,大型客户都有业务全球拓展的需求,考虑到成本,时间因素,一般都是选择云计算,现在云计 ...

  5. Coordinator节点

    Coordinator节点 Coordinator 节点主要负责segment 的管理和分配.更具体的说,它同通过配置往historical 节点 load 或者 drop  segment .Coo ...

  6. struts2之拦截器

    1. 为什么需要拦截器 早期MVC框架将一些通用操作写死在核心控制器中,致使框架灵活性不足.可扩展性降低, Struts 2将核心功能放到多个拦截器中实现,拦截器可自由选择和组合,增强了灵活性,有利于 ...

  7. (function($){….})(jQuery)一种js插件写法

    我们先看第一个括号里边的内容:function($){….},这不就是一个匿名的函数吗?但是它的形参比较奇怪,是$,这里主要是为了不与其它的库冲突. 这样我们就比较容易理解第一个括号内的内容就是定义了 ...

  8. 打印Fibonacci数列方法汇总(前20项,每行5个)

    NO.1 迭代法 标签:通俗.易懂 思路:先打印第一项.再在循环里面执行fib=fib1+fib2,把fib2赋给fib1,把fib赋给fib2,每行5个可使用if函数(循环次数对5取余). #inc ...

  9. lua 数组

    lua 数组 语法结构 arr = { - } 一维数组 数组的值仍然是数组的, 为多维数组, 否则为一维数组 示例程序 local arr = {1, 2, 3} for i = 1, #arr d ...

  10. redis 压缩链表

    redis 压缩链表 概述 压缩链表是相对于普通链表而言的 当普通链表的数据越来越多, 链表查询性能会低效 当存储的数据较少时, 使用链表存储会浪费空间 压缩链表本质上是一个字符串 压缩链表内存储的数 ...