SiteMap  | About    

Vector in Java

Java Vectors are commonly used instead of arrays, because they expand automatically when new data is added to them. That means the Vector instances, like linked-lists, can grow dynamically . However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

How to Java Vector

A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent. Depending on how you use these classes, you could end up taking a large performance hit while adding new elements. Each Vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement . An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.

Vector vs Arraylist

The Vector class is thread-safe. 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. ArrayList is non-synchronized which means multiple threads can work on ArrayList at the same time. So you won't get a ConcurrentModificationException. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.

The following Java program illustrates several of the methods supported by this Vector collection Framework: (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.