Vector源码分析
Vector与ArrayList底层实现基本类似,底层都是用数组实现的,最大的不同是Vector是线程安全的。ArrayList源码分析请参考ArrayList源码分析
一、源码分析
基于jdk1.7源码
属性
protected Object[] elementData;//用来存储元素
protected int elementCount;//元素数量
protected int capacityIncrement;//扩容增量,扩容时增加的容量大小。
Vector多了一个capacityIncrement属性。当Vector需要扩容时,增加的容量大小就是该值。
而ArrayList扩容时是扩充到原容量的1.5倍。
构造器
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
public Vector() {
this(10);
}
public Vector(Collection<? extends E> c) {
elementData = c.toArray();
elementCount = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}
跟ArrayList一样,创建Vector时可以指定初始容量initalCapacity,如果不指定则默认初始容量为10。
Vector也可以在创建时指定扩容增量,如果不指定则为0。
add方法
public synchronized boolean add(E e) {
modCount++;
//确定是否还有容量
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
实现与ArrayList差不多,最大区别是该方法进行了同步。继续往下跟代码。
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
//扩容。指定了扩容增量,则增加该值。没指定,则扩容到原来的两倍
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
//确定增量的最小值和最大值
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
//扩容时移动元素
elementData = Arrays.copyOf(elementData, newCapacity);
}
整个过程基本与ArrayList一样,但是不同点是Vecotor扩容增加的容量大小。
如果Vector初始化时指定了扩容增量,则增加的容量值就是指定的扩容增量,否则,增加1倍容量,也就是扩容到原来的2倍。
get方法
public synchronized E get(int index) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
return elementData(index);
}
很简单,与ArrayList一样,也是先进行边界检查,再根据下标获取元素。
remove方法
public synchronized E remove(int index) {
modCount++;
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index);
int numMoved = elementCount - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--elementCount] = null; // Let gc do its work
return oldValue;
}
跟ArrayList实现差不多,不再赘述。
二、Vector和ArrayList的比较
1.扩容
Vector在创建时能够指定扩容增量,如果指定了该增量,当自动扩容时增加的容量就是该增量值。如果创建时没指定增量(或该值<=0),则扩容1倍,即容量变为原来的2倍。
ArrayList并没有提供能指定增量的构造方法,扩容0.5倍,即容量变为原来的1.5倍(具体参考ArrayList源码分析)
2.线程安全和效率
Vector的修改方法都是线程安全的,然而java.util.concurrent包中对容器提供了线程安全的实现版本,非线程安全时,可以用ArrayList来代替Vector,需要线程安全时可以使用Collections.synchronizedList(new ArrayList())和CopyOnWriteArrayList来替代,也就是说Vector被淘汰了。
ArrayList并不是线程安全的,因此相对来说,ArrayList比Vector效率要高一些。
Vector源码分析的更多相关文章
- Stack和Vector源码分析
Stack和Vector源码分析 Stack和Vector源码分析stack源码分析1.Stack是什么2.Stack的结构图3.Stack继承关系4.Stack的主要方法5.Stack源码Vecto ...
- Vector源码分析和实例应用
1.Vector介绍 Vector 是矢量队列,它是JDK1.0版本添加的类.继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口. Vector ...
- Java集合之Vector源码分析
概述 Vector与ArrayLIst类似, 内部同样维护一个数组, Vector是线程安全的. 方法与ArrayList大体一致, 只是加上 synchronized 关键字, 保证线程安全, 下面 ...
- ArrayList、LinkedList和Vector源码分析
ArrayList.LinkedList和Vector源码分析 ArrayList ArrayList是一个底层使用数组来存储对象,但不是线程安全的集合类 ArrayList的类结构关系 public ...
- [Java]Vector源码分析
第1部分 Vector介绍 Vector简介 Vector也是基于数组实现的,是一个动态数组,其容量能自动增长.继承于AbstractList,实现了List, RandomAccess, Clone ...
- ArrayList和LinkedList和Vector源码分析
ArrayList源码: private static final int DEFAULT_CAPACITY = 10;//默认长度 /** * Shared empty array instance ...
- Stack&Vector源码分析 jdk1.6
参照:http://www.cnblogs.com/tstd/p/5104099.html Stack(Fitst In Last Out) 1.定义 public class Stack<E& ...
- Vector和Stack源码分析/List集合的总结
序言 这篇文章算是在这list接口下的集合的最后一篇了,前面ArrayList.LinkedList都已经讲解完了,剩下就Vector和Vector的子类Stack啦.继续努力.一步一个脚印, --W ...
- 源码分析(5)-ArrayList、Vector和LinkedList(JDK1.8)
一.概述 1.线程安全:ArrayList和LinkedList非线程安全的.Vector线程安全的. 2.底层数据结构:ArrayList和Vector底层数据结构是数组:LinkedList双向链 ...
随机推荐
- C++ 标准 和 C 标准 (截止到2019年03月)
C++ 标准:维基百科 Year C++ Standard Informal name 1998 ISO/IEC 14882:1998[23] C++98 2003 ISO/IEC 14882:200 ...
- DubboAdmin部署
1.软件下载 部署管理后台和监控中心需要以下软件 opensesame 下载地址:https://github.com/alibaba/opensesame Dubbo源码下载 https://g ...
- 后台登录(包含验证码)的php代码实现
login.html文件 <html> <title>login in</title> <body> <form action="han ...
- php学习----基本介绍及数据类型
php 官方手册:http://php.net/manual/zh/ 1.PHP(全称 Hypertext Preprocessor,超文本预处理器的字母缩写)是一种服务器端脚本语言,它可嵌入到 HT ...
- HTTP1.0 、1.1
网上有很多资料说明这个,但都很长的,觉得东西太多也记不住,就记点东西,权当笔记. HTTP 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一 ...
- nginx tcp负载均衡 (Module ngx_stream_upstream_module)
Example ConfigurationDirectives upstream server zone state hash least_conn ...
- MySQL高级知识(六)——索引优化
前言:索引优化的目的主要是让索引不失效,本篇通过相关案例对索引优化进行讲解. 0.准备 创建经典的tb_emp表. DROP TABLE IF EXISTS `tb_emp`; CREATE TABL ...
- MySQL高级知识(五)——索引分析
前言:前面已经学习了explain(执行计划)的相关知识,这里利用explain对索引进行优化分析. 0.准备 首先创建三张表:tb_emp(职工表).tb_dept(部门表)和tb_desc(描述表 ...
- console命令的其他强大用法
阅读目录 谷歌控制台Elements面板查看元素上绑定的事情样式操作总况console.logconsole.infoconsole.errorconsole.warnconsole.debugcon ...
- 转://Oracle 复合压缩索引场景及性能对比
摘要:今天为什么提到这个话题,出于一个偶然,一个同事在优化新开发的系统时向我请教如何添加复合压缩索引的问题.我总结了一下,问题有三. 第一:需不需要压缩 第二:对第几列压缩 第三:性能对比,选出最优 ...