-2

I'm using Vector to store records in my program (that is an XML parser (records are Tags)). Is there any limitation for the no of records you can put in vector because i suspect that my vector does not save all the tags from the file (there are millions of tags in the file). So Is it true? I've also seen most of the people are suggesting to use ArrayList instead of Vector. Both grows automatically and should be able to store an arbitrary amount of elements, then whats the difference?

What should i use?

Thanks in advance.

Subhan
  • 1,544
  • 3
  • 25
  • 58

2 Answers2

2

For new code, always use ArrayList<T>. Vector is synchronized, where as with ArrayList you can choose to make it synchronized or not.

The synchronization overhead of vector (which you might not want) can cause it to be slower than an ArrayList. It's like StringBuffer vs StringBuilder.

If you need synchronization, you can externally synchronize an ArrayList.

David Xu
  • 5,555
  • 3
  • 28
  • 50
  • If you ever need to synchronize a `List`, then you're doing something wrong. Note that even if you decorate your `List` with `Collections#synchronizedList` you can get a `ConcurrentModificationException`. – Luiggi Mendoza Apr 17 '15 at 05:57
1

Vector is thread safe where as ArrayList isn't.

ArrayList is non-synchronized which means multiple threads can work on ArrayList at the same time. For e.g. if one thread is performing an add operation on ArrayList, there can be an another thread performing remove operation on ArrayList at the same time in a multithreaded environment

While Vector is synchronized. This means if one thread is working on Vector, no other thread can get a hold of it. Unlike ArrayList, only one thread can perform an operation on vector at a time.

Performance: ArrayList gives better performance as it is non-synchronized. Vector operations gives poor performance as they are thread-safe, the thread which works on Vector gets a lock on it which makes other thread wait till the lock is released.

Source: http://beginnersbook.com/2013/12/difference-between-arraylist-and-vector-in-java/

Community
  • 1
  • 1
Touchstone
  • 5,575
  • 7
  • 41
  • 48