Vector in Java Collection

Java Vector in Collection:

Java Vector

Vector is a part of the Java Collection framework since the java 1.2 version. It is implements the List interface, It is found in the java.util package. Vector is the best choice if our frequent operation is retrieval operation. SO if we are want to search and get data from the list then Vector is the best option.

If our frequent operation is insertion and deletion in the middle then it worsts choice, because when are deleting or adding any element in the list then shift operation is required and it takes too much time. For insertion and deletion operation we need to use LinkedList.

 

structure of collection vector
From where vector comes

Important Point of Vector

  • The Underlying Data Structure of the Vector is a growable Array or resizable Array.
  • In vector duplicate objects are allowed and insertion order is preserved.
  • null Insertion is Possible.
  • Heterogeneous Objects are allowed.
  • Vector Implements Cloneable, RandomAccess interfaces, and Serializable.
  • Vector is Thread Safe because every method present in Vector is synchronized.

 

Constructors of Java Vector:

Vector v = new Vector();
  • When we are creating an empty Vector object the time default initial capacity of Vector is 10.
  • When Vector reaches its max capacity then a new Vector object will be created with a new capacity.

How to calculate new capacity:

New Capacity = Current Capacity * 2

Suppose Vector current capacity is 10 then the new capacity is 10*2=20, so the new capacity will be 20, so that’s why we can find the new capacity of Vector every time when it reaches its max capacity.

 

Vector v = new Vector(intinitialCapacity);

In the above example, we are creating an empty Vector object with a specified initial capacity.

 

Vector v = new Vector(intinitialCapacity, intincrementalCapacity);

In the above example, if we want to increase the size of the Vector object with some specified capacity using.

 

Vector v = new Vector(Collection c);

It constructs a vector that contains the elements of a collection c.

 

Vector Methods:

There are so many methods available in Vector for adding removing getting.

Add Methods of Vector:

  • add(Object o) -> This method is used to append the specified element in the given vector.                                                    
  • add(int index, Object o) -> Using this method we can insert the specified element at the specified position in a Vector.                                                                                                                                                                                                                     
  • addElement(Object o) -> It is used to append the element of the specified component to the end of this vector. It increases the vector size by one.

 

Note: Using the add() method and addElement() method we are adding elements in vector means both methods are doing the same thing.

 

 

Retrieve elements of Vector:

  • Object get(int index) -> This method is used to get the specifiec index element from Vector.                                                
  • Object firstElement() -> Using this method we can get the first element from Vector.                                                                
  • Object lastElement() -> It is used to get the first element from Vector.

 

Remove elements of Vector:

  • remove(Object o) -> It is used to used to delete the object from Vector.                                                                                 
  • removeElement(Object o) -> This method is used to remove the first (lowest-indexed) occurrence of the argument.                                                                                                                                                                                                        
  • remove(int index) -> It is used to delete the specifiec index element from Vector.                                                                              
  • removeElementAt(int index)  -> This method is used to delete the component at the specified index from Vector.
  • clear() -> Using this method we can delete all of the elements from this vector.                                                                                              
  • removeAllElements() -> This method is used to remove all elements from the vector and then set the size of the vector to zero.

 

Another method of Vector:

  • int size() -> This method is used to get the size of the Vector.
  • int setSize() -> Using this method we can set the size of given vector.
  • int capacity() -> It is used to get the capacity of the vector.
  • Enumeration element() -> Using this method we can itreate the Vector elements.

 

Example:

package com.technicalround;
import java.util.*;
public class VectorDemo1 {
public static void main(String args[]) {
Vector<String> vec = new Vector<String>();
vec.add("Sachin");
vec.add("Rohit");
vec.add("Virat");
vec.add("Rahul");
vec.addElement("Vihari");
vec.addElement("Rahane");
System.out.println("Elements are: " + vec);
}
}

Output:

Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat, Deer]

 

Example:

package com.technicalround;
import java.util.Vector;
class VectorDemo2 {
public static void main(String[] args) {
Vector v = new Vector();
System.out.println("Initial ciapacity:" + v.capacity()); // 10
for (int i = 1; i <= 10; i++) {
v.addElement(i);
}
System.out.println("Ciapacity of after adding 10 element: " +v.capacity()); // 10
v.addElement("Z");
System.out.println(" After reaching max size: "+ v.capacity()); // 20
System.out.println(v); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, A]
}
}

Output:

Initial ciapacity:10
Ciapacity of after adding 10 element: 10
After reaching max size: 20
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, Z]

 

Read More topics related to the collection.

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

 

Leave a Reply

Your email address will not be published.