The best method for accelerating a computer is the one that boosts it by 9.8 m/s2. Anonymous

Vector

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

0 Comments


Definition

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

Character

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.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());
	}

}
						

Output

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

Comparison

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.

0 Comments



Please login to add comments.