Difference Between Similar Terms and Objects

Difference Between ArrayList and Vector

ArrayList vs Vector

A vector implements arrays that can grow/shrink at runtime when some elements are added or removed from it. Its elements are accessed using an integer index. Two fields – capacity and capacity increment, characterize the vector storage management. It implements four interfaces:
* List
* Random Access
* Cloneable
* Serializable interfaces

ArrayList, just like vectors, also implements four interfaces. Again, like vectors, its size can change during runtime. Also, it has a field called capacity whose size is as large as the size of the ArrayList.

Both vectors and ArrayList are good for retrieving elements from any position and also for inserting or deleting elements from the end of the container class.
From the API perspective, both vectors as well as ArrayList are very similar. So where exactly lies the difference between the two? The following points will shed some light on this matter:

1. Synchronization: Vectors are synchronized but ArrayList is not. If you add or delete elements from an ArrayList, it is referred to as structural modification. When multiple threads are accessing an ArrayList concurrently with a block of code modifying the list, then one needs to externally synchronize the list. In short, vector contents are thread-safe whereas ArrayList’s are not. If the requirement does not mention thread-safe collection, then one should opt for ArrayList as synchronization hits performance. Non-synchronized ArrayLists are fast.

2. Data growth: Both ArrayList and vector elements are stored as arrays, but vectors have a default size of 10, and ArrayList has no default size. When one adds an element to either an ArrayList or vector, there is a chance that either class can run out of room. In such a case, by default a Vector doubles its size while the ArrayList size increases by 50%. You can set an increment value in the case of vectors that is not possible for the ArrayList.

3. Traversing the elements: ArrayList has an advantage here as you can access its elements by simply using an index. In the case of vectors, you need to create an iterator to traverse its elements.

Summary:

1.If the container class is to be modified by a single thread or some local variable, then you should use an ArrayList.
2.When the container class is accessed by multiple threads, then use vectors otherwise you will have to do manual synchronization.
3.We can specify the increment size with the vector and with the ArrayList we can’t.
4.A vector is synchronized and an ArrayList is not.
5.A vector can increment the size by double; ArrayList can increment it by 50%.


Search DifferenceBetween.net :

Custom Search


Help us improve. Rate this post! 1 Star2 Stars3 Stars4 Stars5 Stars
Loading...

Email This Post Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.



Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Articles on DifferenceBetween.net are general information, and are not intended to substitute for professional advice. The information is "AS IS", "WITH ALL FAULTS". User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.


See more about :
Protected by Copyscape Plagiarism Finder