Java Vector

Java Vector is a class that implements the List interface and belongs to the java.util.* package.

A Vector is a better version of an array where it internally uses an array to represent story elements and provides us with the flexibility to resize the list size internally after adding a new element.

In vector, all elements are stored in contiguous locations like arrays. Like an ArrayList, the vector also maintains insertion order.

A vector dynamically grows and shrinks. Vector also allows us to store and perform different actions on the stored data. It allows us to store all types of data as well as null values.

Vector is thread-safe and synchronized in nature.

Syntax:

Vector<String> list = new Vector<String>();
Vector list = new Vector();

The default capacity of the vector is 10. As an array gets initialized with some given size during creation, Similarly, Vector also gets initialized with the default capacity of 10. Size and capacity are mainly two different things. Size refers to the number of elements inserted in the vector or list, and capacity refers to the initialization of the vector.

At the creation of Vector, it initializes with a default capacity of 10 and a size of 0. As soon as we start inserting elements into the vector, the size will grow and the capacity will remain the same as 10 till the addition of the 10th element. The capacity will change to 20, which is double once we insert the 11th element.

Constructors:

  • Vector can also initialize with the capacity of 3.

Vector list = new Vector(3);

  • Initialize vector with initiial capacity and increment.

Vector list = new Vector(int initialCapacity, int increment);

  • The below expression will initialize the vector with default capacity of 3 and on addition of 4th element the size will get increase by 2 which will get update to 5 (3+2).

Vector list = new Vector(3, 2);

Below are the list of methods supported by LinkedList class to perform different operations.

Add Elements to a Vector:

We can use add() method to add elements in the vector.

list.add("first");

Add element in list at specific location or index using add() method:

list.add(1, "first");

Update Element in Vector:

Update element in list at specific location or index using set() method:

list.set(1, "first");

Remove Elements from Vector:

We can use remove() method to remove element from the vector.

list.remove("first");

remove element in list from specific location or index using remove() method:

list.remove(1);

Example:

Below is the example to understand some of the methods which are defined in ArrayList class.

Please read out the comments also to get better understanding of below given code snippet.

Sorting:

Collections sort() method provides us the capability to sort Array list elements alphabetically.

Vector Methods:

size(): The size() method will return the number of elements present in the vector.

clear(): The clear() method will make list empty and remove all the elements from list.

contains(): The contains() method check either the particular element is exist or not in the list.

isEmpty(): Method returns true if we don’t have any element in the list.

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

πŸ“ Blogs

πŸ“§ Imran Khan