Friday, 9 December 2016

Vector in java

Vector belongs to java.util package. Vector is a class that extends AbstractList and implements List interface, RandomAccess interface, Serializable interface and Cloneable interface. It also implements grow able array of objects. The elements in vector can be accessed using integer index just as we do in array.

Vector is synchronized and thus should be preferred over ArrayList if thread-safety is concern of the developer. 
Due to synchronized nature, it does not allow multiple threads to access and modify itself concurrently. If one thread has access to the vector then other thread wanting to access and operate on it must wait for its turn until the vector is released by the thread in action.

On other hand, this synchronized nature affects Vector’s performanceSo ArrayList is preferred over Vector if performance is to be considering factor.


·         Vector vs ArrayList

Now let’s look at the ways in which you can create vector in java. 

·         Vector myVector = new Vector<E>();
·         Vector myVector = new Vector<E>(Collection c);
·         Vector myVector = new Vector<E>( (int initialCapacity);
·         Vector myVector = new Vector<E>(int initialCapacityint capacityIncrement);

Now let’s look at different ways in which you can use the Vector that you created.

Vector operations
·         Sort the elements in vector
·         Search element in vector
·         Copy element of vector to another vector
·         Remove element from vector
·         Remove all elements from the vector
·         Remove element from a particular index in vector

Traverse Vector
·         Traverse vector using Iterator
·         Traverse vector using ListIterator
·         Traverse Vector using Enumeration

·         Vector to ArrayList
·         Vector to Array
·         Vector to LinkedList

No comments:

Post a Comment

If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.