美文网首页Java语言
集合类中的ArrayList和Vector的区别

集合类中的ArrayList和Vector的区别

作者: 李2牛 | 来源:发表于2018-03-12 19:12 被阅读1次

标准的解释:

  1. ArrayList不是线程安全的,Vector是线程安全的
  2. 底层数组容量不够的时候,ArrayList拓展为原来的1.5倍,vector拓展为原来的2倍。

细细对比:

  1. 源码实现:
    ArrayList的底层实现:


    ArrayList的源码实现

    Vector的底层实现:


    Vector的源码实现
  2. 方法的对比;
    Vector的重要方法
    get(……)
    add(……)
    remove(……)
    isEmpty(……)
    size(……)
    capacity(……)
    ······


    Vector的几种方法

都使用了synchronized关键字,操作中会加锁释放锁保证线程安全,但是会为系统带来更多的开销。
ArrayList则没有该关键字,所以线程不安全。


ArrayList的方法

再看一点扩容


ArrayList的扩容方法grow
Vector的扩容方法grow

1.5和2的对比

  1. 线程安全牺牲了时间空间,线程不安全效率上占优势。鱼与熊掌不可兼得。
    类似的组合还有:
- 线程安全 非线程安全
StringBuilder
StringBuffer
HashMap
HashTable

相关文章

网友评论

    本文标题:集合类中的ArrayList和Vector的区别

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