Difference between ArrayList and Vector in Java

Source: Internet
Author: User

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 is few differences in the the "the" and "the" and "the" data. In this post we'll 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 a add operation on ArrayList, there can are an another thread performing remove opera tion on ArrayList @ the same time in a multithreaded environment

While Vector is synchronized. This means if one thread is the working on Vector, and 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, Howe Ver the They resized is different. ArrayList grow by half of it size when resized and 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 is 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 Iterato R would 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 are not fail-fast. The other side the iterator and Listiterator returned by ArrayList is Fail-fast.

5) who belongs to collection framework really? The vector was wasn't the part of the collection framework, it had been included in collections later. It can be considered as Legacy code. There is nothing about the 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 sections of this post or U Se copyonwritearraylist which is a Thread-safe variant of ArrayList.

There is 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 (vectors and ArrayList) are fail-fast.
    3. They both is 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 was your best bet as it ensures that one thread acces s the collection at a time.

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

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

Use Collecions.synzhonizedlist methodListList= Collections.Synchronizedlist(New ArrayList());...If you wanna use iterator on the synchronized list, use itLike this. It should is in synchronized block.synchronized (list) { Iterator Iterator = list.  Iterator();  while (iterator.  Hasnext()) ... iterator.  Next();  ... }< /c13> 
Ref

Difference between ArrayList and Vector in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.