Java - Difference between ArrayList and Vector in Java
2015-10-08 15:52
549 查看
http://javarevisited.blogspot.de/2011/09/difference-vector-vs-arraylist-in-java.html
Because sometimes I can't open this page without proper proxy configuration, so I have to copy some content here.
Similarities between ArrayList and Vector
1) Vector and ArrayList are index based and backed up by an array internally.
2) Both ArrayList and Vector maintains the insertion order of element. that's means you can assume that you will get the object in the order you have inserted if you iterate over ArrayList or Vector.
3) Both Iterator and ListIterator returned by ArrayList and Vector are fail-fast.
4) ArrayList and Vector also allows null and duplicates.
Difference between Vector and ArrayList
Differences are based upon properties like synchronization, thread safety, speed, performance , navigation and Iteration over List etc.
1) Synchronization and thread-safety
First and foremost difference between Vector and ArrayList is that Vector is synchronized and ArrayList is not. It means that all the method which structurally modifies Vector e.g. add () or remove () are synchronized which makes it thread-safe and allows it
to be used safely in a multi-threaded and concurrent environment. On the other hand ArrayList methods are not synchronized thus not suitable for use in multi-threaded environment.
2) Speed and Performance
ArrayList is faster than Vector. Since Vector is synchronized and thread-safe it pays price of synchronization which makes it little slow. On the other hand ArrayList is not synchronized and fast which makes it obvious choice in a single-threaded access environment.
You can also use ArrayList in a multi-threaded environment if multiple threads are only reading values from ArrayList or you can create read only ArrayList as well.
3) Capacity
Whenever Vector crossed the threshold specified, it increases itself by value specified in capacityIncrement field while you can increase size of ArrayList by calling ensureCapacity () method.
4) Enumeration and Iterator
Vector can return enumeration of items which it holds by calling elements () method. This enumeration is not fail-fast as opposed to Iterator and ListIterator returned by ArrayList.
5) Legacy
Another point worth to remember is Vector is one of those classes which comes with JDK 1.0 and initially not part of Collection framework but in later version it's been re-factored to implement List interface so that it could become part of collection framework.
After considering these points about both Vector and ArrayList , my conclusion is use ArrayList wherever possible and avoids use of Vector until you have no choice. Think for CopyOnWriteArrayList over Vector, if you have multiple
readers and few writers because it can provide thread-safety without impacting performance too much.
Because sometimes I can't open this page without proper proxy configuration, so I have to copy some content here.
Similarities between ArrayList and Vector
1) Vector and ArrayList are index based and backed up by an array internally.
2) Both ArrayList and Vector maintains the insertion order of element. that's means you can assume that you will get the object in the order you have inserted if you iterate over ArrayList or Vector.
3) Both Iterator and ListIterator returned by ArrayList and Vector are fail-fast.
4) ArrayList and Vector also allows null and duplicates.
Difference between Vector and ArrayList
Differences are based upon properties like synchronization, thread safety, speed, performance , navigation and Iteration over List etc.
1) Synchronization and thread-safety
First and foremost difference between Vector and ArrayList is that Vector is synchronized and ArrayList is not. It means that all the method which structurally modifies Vector e.g. add () or remove () are synchronized which makes it thread-safe and allows it
to be used safely in a multi-threaded and concurrent environment. On the other hand ArrayList methods are not synchronized thus not suitable for use in multi-threaded environment.
2) Speed and Performance
ArrayList is faster than Vector. Since Vector is synchronized and thread-safe it pays price of synchronization which makes it little slow. On the other hand ArrayList is not synchronized and fast which makes it obvious choice in a single-threaded access environment.
You can also use ArrayList in a multi-threaded environment if multiple threads are only reading values from ArrayList or you can create read only ArrayList as well.
3) Capacity
Whenever Vector crossed the threshold specified, it increases itself by value specified in capacityIncrement field while you can increase size of ArrayList by calling ensureCapacity () method.
4) Enumeration and Iterator
Vector can return enumeration of items which it holds by calling elements () method. This enumeration is not fail-fast as opposed to Iterator and ListIterator returned by ArrayList.
5) Legacy
Another point worth to remember is Vector is one of those classes which comes with JDK 1.0 and initially not part of Collection framework but in later version it's been re-factored to implement List interface so that it could become part of collection framework.
After considering these points about both Vector and ArrayList , my conclusion is use ArrayList wherever possible and avoids use of Vector until you have no choice. Think for CopyOnWriteArrayList over Vector, if you have multiple
readers and few writers because it can provide thread-safety without impacting performance too much.
相关文章推荐
- Jquery和JS获取ul中li标签
- extjs初学者教程
- Browserify - 像Node.js的require()一样组织浏览器端的js代码
- Java - Difference between Wait and Sleep , Yield in Java
- 在实现文本框只能输入数字和小数点的基础上实现了价格样式(保留两位小数)
- img及父元素(容器)实现类似css3中的background-size:contain / background-size:cover
- angularjs input上传图片前获取图片的Size
- 【笔记】利用CSS3特性实现圆角DIV
- Javascript DOM
- setTimeout,setInterval,process.nextTick,setImmediate in Nodejs
- VM11 vmware-tools 共享文件夹 报错
- bootstrap中fuelux.tree的简单使用
- JS获取浏览器型号
- C#中js无效,不弹出提示语句,与ScriptManager冲突导致
- 经典收藏 50个jQuery Mobile开发技巧集萃
- HTML
- nodejs中exports与module.exports的实践
- jQuery Mobile案例,最近用Moon.Web和Moon.Orm做了一套系统
- javascript,一道容易犯错的小题
- button跳转jsp