In software, we rarely have meaningful requirements. Even if we do, the only measure of success that matters is whether our solution solves the customerís shifting idea of what their problem is. Jeff Atwood


Language Java | Level Intermediate | Category Java collections framework (JCF) | 2015-07-22 20:20:28



Vector is a dynamic array which contains the elements that can be accessed using an index. Vector is a legacy class which supports synchronization.


Vector can grow and shrink dynamically to maintain the optimal use of storage.
Storage maintains capacity and capacityIncrement. The capacity is always at least as large as the vector size.
The fail-fast behavior of iterators should be used only to detect bugs.
Implemented using List interface.

source Code

package com.careerdrill.collections.list;

import java.util.Vector;

public class VectorUse {

	public static void main(String[] args) {
		Vector< Integer > v = new Vector< Integer >(3, 2);
		System.out.println("Initial size: " + v.size());
		System.out.println("Initial capacity: " +
		v.addElement(new Integer(1));
		v.addElement(new Integer(2));
		v.addElement(new Integer(3));
		v.addElement(new Integer(4));
		System.out.println("Capacity after four additions: " + v.capacity()); 
		System.out.println("Elements from Vector: " + v.toString());



Initial size: 0
Initial capacity: 3
Capacity after four additions: 5
Elements from Vector: [1, 2, 3, 4]


Vector Array List
Vector is synchronized. ArrayList is non-synchronized.
It supports synchronization. So, less performance compares than array list. ArrayList gives better performance.
iterators returned by this class's iterator and listIterator methods are fail-fast. iterators returned by this class's iterator and listIterator methods are fail-fast.
Part of collection framework. Part of collection framework.
By default support synchronization. Collections.synchronizedList use to synchronize the elements.

When to use

If there is a need to perform "thread-safe" operation, then use the vector. It allows only one thread at a time.


Please login to add comments.