Vector in Java with Example | Java Vector Class Declaration, Constructors, Methods, Elements Examples

Seeking help from Java Vector Class with Example Tutorial, makes you understand how to create a Vector Class in Java? Also, we will learn all about vector class, its declaration, how to use it, how it differs from the ArrayList, etc. So, keep reading this tutorial and gain more information like Java Vector Constructors, Methods, Advantages, Add Elements & Remove Elements. For better learnings, we have shared some useful Java Vector Examples with Outputs.

This Tutorial on Vector Class in Java Includes:

Java Vector Class

A vector in Java is a class that is present in java.util package and implements the List interface. Vector implements a dynamic array which means we can grow or shrink the size of the array as per our need. Vector is similar to ArrayList, but with two major differences:

  1. The vector is synchronized, hence the vector object is thread-safe.
  2. It contains many legacy methods which are not part of the collection framework.

Vector in Java with Example 1

Creating a Vector Class in Java

To create a Vector Class in Java, we have to use the following vector constructor methods:

Constructor Prototype Description
vector() This vector() is the default constructor of the Vector class. It generates an empty vector with size 10.
vector(int initialCapacity) This overloaded constructor constructs an empty Vector object with the capacity = initialCapacity.
vector(int initialCapacity, int capacityIncrement) This constructor method creates an empty Vector object by specified initialCapacity & capacityIncrement.
Vector( Collection< ? extends E> c) A Vector object is formed with the initial elements from specified collection c.

Here is how we can create Java vectors.

creating a vector in java

In the above syntax, the Type indicates the linked list type. For Example:

java vector class creation with explanation

To use Vectors, we first have to import the Vector class from java.util package

Java Vector Class Declaration

public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

Here, E is the type of element.

  • Directly, the known subclass of the Java vector is Stack.
  • It extends AbstractList and implements List interfaces.
  • It implements Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess interfaces.

Do Check: HashSet Class in Java with Example

Java Vector Constructors

Java Vector class contains four constructors. Here are the Vector constructors.

1. Vector(): The first constructor is used to creates a default vector, which has an initial size of 10.

2. Vector(int size): The second constructor is used to creates a vector whose initial capacity is specified by size.

3. Vector(int size, int incr): The third constructor is used to creates a vector whose initial capacity is specified by size and whose increment is specified by incr.

4. Vector(Collection c): The fourth constructor is used to creates a vector that contains the elements of collection c.

All the vectors start with an initial capacity. After this initial capacity is reached, the next time that you attempt to store an object in the vector, the vector automatically allocates space for those objects.

Methods of Vector Class in Java

Some of the important methods of the Vector class is given below:

1. void addElement(Object element): This method is used to insert the element at the end of the vector.

2. int capacity(): This method is used to find out the current capacity of the vector.

3. boolean contains(Object element): This method is used to check the specified element present in the vector or not. If the element is found it returns true else false.

4. Object elementAt(int index): This method returns the element which is present at the specified index.

5. Object firstElement(): This method is used to retrieve the first element from the vector.

6. Object lastElement(): This method is used to retrieve the last element from a vector.

7. boolean isEmpty(): This method is used to check the vector is empty or not. It returns true if the vector doesn’t have any element.

8. int indexOf(Object element): This method is used to find the index of the specified element. If the specified element is not in the vector it returns -1.

9. void insertElementAt(Object element, int index): This method is used to insert the specified element at the specified index.

10. void removeAllElements(): This method is used to remove all the elements from the vector.

11. boolean removeElement(Object element): This method is used to remove the specified element from the vector.

12. void removeElementAt(int index): This method is used to remove the element at the specified index.

13. void setElementAt(Object element, int index): This method is used to set the element at the specified index.

14. void setSize(int size): This method is used to set the size of the given vector. If the new size is less than the old size then elements are lost. If the new size is larger than the old size then null elements are added.

15. int size(): This method returns the number of elements currently in the vector.

16. int capacity(): This method returns the current capacity of the vector.

17. int hashcode(): This method returns the hashcode value of the vector.

18. Object get(int index): This method returns an element that is present at the specified index.

19. Object[] toArray(): It is used to return an array containing all of the elements in this vector in the correct order.

20. Object[] toArray(Object[] a): This method of java vector is used to return an array including all of the elements in this vector in the correct order; the runtime type of the returned array is that of the specified array.

Java Vector Examples

On this Vector Class in Java Tutorial, beginners who are learning this Java Vector Concept can find examples on how to add elements, remove elements, get a hashcode in vector, converting a vector into List & ArrayList using Vector Class in Java package. Take a look at the below sections and grab the opportunity of learning the topic thoroughly using the java vector example programs.

Add Elements to Vector in Java Example

import java.util.*;
class Example
{
public static void main(String args[])
{
//creating a vector
Vector vec = new Vector(5);

//adding element to the vector
vec.addElement("Sumit");
vec.addElement("Amit");
vec.addElement("Rohit");
vec.addElement("Rahul");
vec.addElement("Ajay");

//check size and capacity of a vector

System.out.println("Size of the Vector is: " +vec.size());

System.out.println("Default increment of a Vector is: " +vec.capacity());

vec.addElement("Vijay");
vec.addElement("Prashant");
vec.addElement("Raaj");

//again check size and capacity of a vector after adding 3 element

System.out.println("After the addition the size is: " +vec.size());

System.out.println("The increment of vector is: " +vec.capacity());

}
}

Output:

The size of the vector is: 5
Default increment of a vector is: 5
After the addition, the size is: 8
The increment of the vector is: 10

Remove Elements Java Vector Example

import java.util.*;

class Example
{
public static void main(String args[])
{
//creating a vector
Vector vec = new Vector();

//adding element to the vector
vec.addElement("Sumit");
vec.addElement("Amit");
vec.addElement("Rohit");
vec.addElement("Rahul");
vec.addElement("Ajay");
vec.addElement("Vijay");
vec.addElement("Raam");

System.out.println("Elements of Vector before remove() is:\n ");
for(int i=0; i<vec.size(); i++)
{
System.out.println(vec.get(i));
}

//remove two elements
vec.remove("Amit");
vec.remove("Rahul");

System.out.println("Elements of vector after remove() is \n");
for(int i=0; i<vec.size(); i++)
{
System.out.println(vec.get(i));
}

Object obj = vec.remove(2);
System.out.println("Element of vector at a given index is \n");
System.out.println(obj);

//removing all the elements
vec.clear();
System.out.println("Size of the vector after removing all the elements" + vec.size());}
}

Output:

Elements of the vector before remove():
Sumit
Amit
Rohit
Rahul
Ajay
Vijay
Raam

Elements of the vector after remove():
Sumit
Rohit
Ajay
Vijay
Raam

Element of the vector at a given index: Ajay
Size of the vector after removing all the elements: 0

Getting Hashcode of the Vector in Java Example

import java.util.*;

class Example
{
public static void main(String args[])
{

//creating a vector
Vector vec = new Vector();

//adding element to the vector
vec.addElement("Sumit");
vec.addElement("Amit");
vec.addElement("Rohit");
vec.addElement("Rahul");
vec.addElement("Ajay");
vec.addElement("Vijay");
vec.addElement("Raam");

//get the hashcode of this vector
System.out.println("Hashcode of this vector is: " +vec.hashCode());

}
}

Output:

The Hashcode of this vector is: 938853329

Converting Vector into a List Example

We can convert a vector into a List by calling Collections.list(vec.elements()), It returns a List object.

import java.util.*;

class Example

{

public static void main(String args[])

{

//creating a vector

Vector vec = new Vector();

//adding the element to the vector

vec.addElement("Sumit");

vec.addElement("Amit");

vec.addElement("Rohit");

vec.addElement("Rahul");

vec.addElement("Ajay");

vec.addElement("Vijay");

vec.addElement("Raam");

System.out.println("Vector elements are: \n" +vec);

//converting vector to list

List list = Collections.list(vec.elements());

System.out.println("List elements are:\n " +list);

}

}

Output:

Vector elements are:
[Sumit, Amit, Rohit, Rahul, Ajay, Vijay, Raam]
List elements are:
[Sumit, Amit, Rohit, Rahul, Ajay, Vijay, Raam]

Java Vector Example to convert a Vector into an ArrayList

We can convert a vector into an ArrayList by declaring an array list object using a vector object.

import java.util.*;

class Example

{

public static void main(String args[])

{

//creating a vector
Vector vec = new Vector();

//adding element to the vector
vec.addElement("Sumit");

vec.addElement("Amit");

vec.addElement("Rohit");

vec.addElement("Rahul");

vec.addElement("Ajay");

vec.addElement("Vijay");

vec.addElement("Raam");

System.out.println("Vector elements are: " +vec);

//converting a vector into an array list

ArrayList al = new ArrayList(vec);

System.out.println("ArrayList elements are: " +al);

}

}

Output:

Vector elements are:
[Sumit, Amit, Rohit, Rahul, Ajay, Vijay, Raam]
ArrayList elements are:
[Sumit, Amit, Rohit, Rahul, Ajay, Vijay, Raam]

Leave a Comment