Difference between ArrayList and Vector In java

Reference: http://beginnersbook.com/2013/12/difference-between-arraylist-and-vector-in-java/

JAVA COLLECTIONS

ArrayList and Vector both use Array as a data structure internally. However there are few differences in the way they store and process the data. In this post we will discuss the difference and similarities between ArrayList and Vector.

ArrayList Vs Vector:

1) Synchronization: ArrayList is non-synchronized which means multiple threads can work on ArrayList at the same time. For e.g. if one thread is performing an add operation on ArrayList, there can be an another thread performing remove operation on ArrayList at the same time in a multithreaded environment

while Vector is synchronized. This means if one thread is working on Vector, no other thread can get a hold of it. Unlike ArrayList, only one thread can perform an operation on vector at a time.

2) Resize: Both ArrayList and Vector can grow and shrink dynamically to maintain the optimal use of storage, however the way they resized is different. ArrayList grow by half of its size when resized while Vector doubles the size of itself by default when grows.

3) Performance: ArrayList gives better performance as it is non-synchronized. Vector operations gives poor performance as they are thread-safe, the thread which works on Vector gets a lock on it which makes other thread wait till the lock is released.

4) fail-fast: First let me explain what is fail-fast: If the collection (ArrayList, vector etc) gets structurally modified by any means, except the add or remove methods of iterator, after creation of iterator then the iterator will throw ConcurrentModificationException. Structural modification refers to the addition or deletion of elements from the collection.

As per the Vector javadoc the Enumeration returned by Vector is not fail-fast. On the other side the iterator and listIterator returned by ArrayList are fail-fast.

5) Who belongs to collection framework really? The vector was not the part of collection framework, it has been included in collections later. It can be considered as Legacy code. There is nothing about Vector which List collection cannot do. Therefore Vector should be avoided. If there is a need of thread-safe operation make ArrayList synchronized as discussed in the next section of this post or use CopyOnWriteArrayList which is a thread-safe variant of ArrayList.

There are few similarities between these classes which are as follows:

  1. Both Vector and ArrayList use growable array data structure.
  2. The iterator and listIterator returned by these classes (Vector and ArrayList) are fail-fast.
  3. They both are ordered collection classes as they maintain the elements insertion order.
  4. Vector & ArrayList both allows duplicate and null values.
  5. They both grows and shrinks automatically when overflow and deletion happens.

When to use ArrayList and when to use vector?

It totally depends on the requirement. If there is a need to perform “thread-safe” operation the vector is your best bet as it ensures that only one thread access the collection at a time.

Performance: Synchronized operations consumes more time compared to non-synchronized ones so if there is no need for thread safe operation, ArrayList is a better choice as performance will be improved because of the concurrent processes.

How to make ArrayList synchronized?
As I stated above ArrayList methods are non-synchronized but still if there is a need you can make them synchronized like this –

//Use Collecions.synzhonizedList method
List list = Collections.synchronizedList(new ArrayList());
...

//If you wanna use iterator on the synchronized list, use it
//like this. It should be in synchronized block.
synchronized (list) {
  Iterator iterator = list.iterator();
  while (iterator.hasNext())
      ...
      iterator.next();
      ...
}

Ref

时间: 2024-12-20 14:46:52

Difference between ArrayList and Vector In java的相关文章

Java——(五)Collection之List集合、ArrayList和Vector实现类

------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 一.List集合 List集合代表一个元素有序.客重复的集合,集合中每个元素都有其对应的顺序索引.List 集合允许使用重复元素,可以通过索引来访问指定位置的集合元素.List集合默认按元素的添加 顺序设置元素的索引,例如第一次添加的元素索引为0,第二次添加的元素索引为1...... 1.List接口和ListIterator接口 List作为Collection接口的子接口,所以可以使用Co

Java中 ArrayList、Vector和LinkedList 的使用和详解!

转自:[http://blog.csdn.net/kevon_sun/article/details/7936329] 1 import java.util.*; 2 3 /** 4 * (1)ArrayList是最常用的List实现类,内部是通过数组实现的,它允许对元素进行快速随机访问.数组的缺点是每个元素之间不能含有“空隙”. 5 * 当数组大小不满足时会增加存储能力,将已有数组数据复制到新的存储空间中.当从ArrayList的中间位置插入或者删除元素时,需要对数组进行拷贝,移动,代价比较高

Java ArrayList、Vector和LinkedList等的差别与用法(转)

Java ArrayList.Vector和LinkedList等的差别与用法(转) ArrayList 和Vector是采取数组体式格式存储数据,此数组元素数大于实际存储的数据以便增长和插入元素,都容许直接序号索引元素,然则插入数据要设计到数组元素移动等内存操纵,所以索引数据快插入数据慢,Vector因为应用了synchronized办法(线程安然)所以机能上比ArrayList要差,LinkedList应用双向链表实现存储,按序号索引数据须要进行向前或向后遍历,然则插入数据时只须要记录本项的

【java】ArrayList、Vector、LinkedList的区别

ArrayList.Vector.LinkedList同属于List接口,都存在于java.util包中,都是可伸缩数组,可以动态改变长度. ★ArrayList和Vector都是基于存储元素Object[] array来实现的,它们会在内存中开辟一段连续的空间来存储,因为数据存储是连续的,所以它们支持用下标的方式来访问数据,而且索引数据的速度会比较快,也正因为存储是连续的,在其中插入新的元素时需要移动数据,所以在插入数据时执行速度会比较慢. 在未定义容量大小时,ArrayList和Vector

Java中List,ArrayList、Vector,map,HashTable,HashMap区别用法

Java中List,ArrayList.Vector,map,HashTable,HashMap区别用法 标签: vectorhashmaplistjavaiteratorinteger ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,都允许直接序号索引元素,但是插入数据要设计到数组元素移动 等内存操作,所以索引数据快插入数据慢,Vector由于使用了synchronized方法(线程安全)所以性能上比ArrayList要差,Linke

Java集合(2)——深入理解ArrayList、Vector和LinkedList

回顾 Java集合主要分为两个体系结构,Collection和Map.这篇博客主要介绍Collection子接口List下的三个经常使用的实现类:ArrayList.Vector和LinkedList. 详细内容参见<Java基础--集合> 先看下关系图: 1.ArrayList 这是List最常用的实现类,想一想为什么他最常用? Array,在java中意为"数组".猜想ArrayList和数组应该关系很密切,其实ArrayList可以看作是一个可以改变大小的数组. 举个

java集合之列表:ArrayList、Vector、LinkedList

1 package com.jdk7.chapter4; 2 3 import java.util.ArrayList; 4 import java.util.LinkedList; 5 import java.util.List; 6 import java.util.ListIterator; 7 import java.util.Stack; 8 import java.util.Vector; 9 /** 10 * java中集合的形式一>列表(有序允许重复) 11 * 列表的三种表现形

详解Java中ArrayList、Vector、LinkedList三者的异同点

一.ArrayList ArrayList是一个可以处理变长数组的类型,这里不局限于"数"组,ArrayList是一个泛型类,可以存放任意类型的对象.顾名思义,ArrayList是一个数组列表,因此其内部是使用一个数组来存放对象的,因为Object是一切类型的父类,因而ArrayList内部是有一个Object类型的数组类存放对象.ArrayList类常用的方法有add().clear().get().indexOf().remove().sort().toArray().toStri

深入解析 Java集合类ArrayList与Vector的区别

集合类分为两个分支,Collection与Map,其中Collection接口继承了Iterator接口,继承Iterator接口的类可以使用迭代器遍历元素(即Collection接口的类都可以使用),今天我们从相同点.不同点.以及JDK源码等各个方面来深入解析下,底层使用数组实现的两个集合类:ArrayList与Vector的区别与联系 区别与联系: 1.ArrayList出现于jdk1.2,vector出现于1.0.两者底层的数据存储都使用的Object数组实现,因为是数组实现,所以具有查找