Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between ArrayList and Vector?

What are the differences between the two data structures ArrayList and Vector, and where should you use each of them?

like image 924
KushalP Avatar asked Jun 06 '10 23:06

KushalP


People also ask

What is difference between ArrayList and Vector and LinkedList?

The fundamental difference of the three data structures above is the way they store their data which causes different performance for different operations. In Java (and also used in Kotlin), ArrayList and Vector uses an Array to store its elements, while LinkedList stores its elements in a doubly-linked-list.

What is the difference between list and Vector?

In vector, each element only requires the space for itself only. In list, each element requires extra space for the node which holds the element, including pointers to the next and previous elements in the list.

What is the difference between ArrayList and Vector Mcq?

1) ArrayList is not synchronized. Vector is synchronized. 2) ArrayList increments 50% of current array size if the number of elements exceeds from its capacity. Vector increments 100% means doubles the array size if the total number of elements exceeds than its capacity.

Which is better Vector or ArrayList?

performance – Largely due to synchronization, Vector operations are slower when compared to ArrayList. framework – Also, ArrayList is a part of the Collections framework and was introduced in JDK 1.2.


2 Answers

Differences

  • Vectors are synchronized, ArrayLists are not.
  • Data Growth Methods

Use ArrayLists if there is no specific requirement to use Vectors.

Synchronization

If multiple threads access an ArrayList concurrently then we must externally synchronize the block of code which modifies the list either structurally or simply modifies an element. Structural modification means addition or deletion of element(s) from the list. Setting the value of an existing element is not a structural modification.

Collections.synchronizedList is normally used at the time of creation of the list to avoid any accidental unsynchronized access to the list.

Data growth

Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

like image 61
Sev Avatar answered Oct 01 '22 00:10

Sev


As the documentation says, a Vector and an ArrayList are almost equivalent. The difference is that access to a Vector is synchronized, whereas access to an ArrayList is not. What this means is that only one thread can call methods on a Vector at a time, and there's a slight overhead in acquiring the lock; if you use an ArrayList, this isn't the case. Generally, you'll want to use an ArrayList; in the single-threaded case it's a better choice, and in the multi-threaded case, you get better control over locking. Want to allow concurrent reads? Fine. Want to perform one synchronization for a batch of ten writes? Also fine. It does require a little more care on your end, but it's likely what you want. Also note that if you have an ArrayList, you can use the Collections.synchronizedList function to create a synchronized list, thus getting you the equivalent of a Vector.

like image 39
Antal Spector-Zabusky Avatar answered Sep 30 '22 23:09

Antal Spector-Zabusky