Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector vs. ArrayList which is better? [duplicate]

Possible Duplicate:
Why is Java Vector class considered obsolete or deprecated?

Which type is better to use and how to choice right type (memory usage, execution...)?

like image 668
Ballon Avatar asked Jul 07 '12 13:07

Ballon


People also ask

Is Vector better than ArrayList?

Performance: ArrayList is faster. Since it is non-synchronized, while vector operations give slower performance since they are synchronized (thread-safe), if one thread works on a vector, it has acquired a lock on it, which forces any other thread wanting to work on it to have to wait until the lock is released.

Does ArrayList provides faster random access compared to Vector?

Thats why the Vector object is already synchronized when it is created . Vector is slow as it is thread safe . In comparison ArrayList is fast as it is non synchronized . Thus in ArrayList two or more threads can access the code at the same time , while Vector is limited to one thread at a time.

Does Java Vector allow duplicates?

Vector & ArrayList both allows duplicate and null values. They both grows and shrinks automatically when overflow and deletion happens.

What is the main difference between an ArrayList and a Vector?

ArrayList is non-synchronized. Vector is synchronized. ArrayList increments 50% of its current size if element added exceeds its capacity. Vector increments 100% of its current size if element added exceeds its capacity.


2 Answers

You should normally use ArrayList - it offers better performance.

Vector has just one "advantage" - it is synchronised for concurrent modification. But it turns out in practice that this feature isn't very useful because Vector synchronises at the level of each individual operation. If you are writing concurrent code, you typically need to lock at a much higher level of granularity than an individual collection class.

As a result, Vector is often considered deprecated nowadays.

like image 64
mikera Avatar answered Oct 12 '22 05:10

mikera


As per this question Vector is considered "obsolete", use ArrayList instead.

like image 23
Pere Villega Avatar answered Oct 12 '22 07:10

Pere Villega