What is a Vector in Java?
- In Java, a Vector is a dynamic array-like data structure that can develop or shrivel in size as components are added or removed.
- It is part of the java.util package and executes the List interface, which makes it a part of Java’s collection framework.
- Not at all like arrays, which have a fixed size, a Vector can alter its size dynamically as required, giving more adaptability when working with collections of information.
Characteristics of a Vector in Java:
1.Resizable:
- A Vector consequently resizes itself when components are added past its current capacity.
- At first, a Vector includes a default size, but when it runs out of space, it increments its capacity, more often than not by multiplying its size.
2.Thread-Safety:
- One of the outstanding features of a Vector is that it is synchronized.
- This implies it is thread-safe by default, making it reasonable for utilize in multi-threaded applications.
- In any case, this thread-safety can some of the time make Vector slower than other collections (like ArrayList) that are not synchronized.
3.Ordered:
- Like other List executions, a Vector keeps up the arrange of components as they are inserted.
4.Indexed Access:
- A Vector permits random access to its components through an index, comparable to arrays.
Types of Vectors in Java
- The Vector class itself could be a single class, but it can be utilized in different ways to store distinctive types of information.
- Whereas the Vector class doesn’t have distinct “types,” you’ll make vectors that store objects of different types (e.g., Vector, Vector, etc.), depending on the type parameter utilized with generics.
- In common, you’ll categorize the utilize of Vector into two wide types based on the information it stores:
1.Homogeneous Vectors:
- These vectors store components of the same type (e.g., integers, strings, or any other object).
- Example:
Vector stringVector = new Vector();
2.Heterogeneous Vectors:
- These vectors can store components of diverse types, but this requires utilizing the non-generic form of Vector (which stores Protest sorts).
- Example:
Vector vector = new Vector();
- Here, the vector can store objects of distinctive types like String, Integer, Double, etc.
Common Operations with Vectors in Java
Vectors support a variety of operations. A few of the foremost commonly utilized operations include:
1.Adding Elements:
- add(E component) – Adds an component to the end of the vector.
- add(int index, E component) – Inserts an component at a particular index.
- Example:
Vector<String> vec = new Vector<>();
vec.add("Java");
vec.add("Python");
vec.add("C++");
2.Removing Elements:
- remove(Object obj) – Removes the first occurrence of the required object.
- remove(int index) – Removes the component at the desired index.
- Example:
vec.remove("Python"); // Removes "Python"
3.Accessing Elements:
- get(int index) – Recovers the component at the desired index.
- Example:
String language = vec.get(0); // Retrieves "Java"
4.Checking the Size and Capacity:
- size() – Returns the number of components within the vector.
- capacity() – Returns the current capacity of the vector.
- Example:
System.out.println("Size: " + vec.size());
System.out.println("Capacity: " + vec.capacity());
5.Other Operations:
- contains(Object o) – Checks in case the vector contains a particular component.
- clear() – Removes all components from the vector.
- isEmpty() – Checks in case the vector is empty.
- Example:
vec.clear(); // Clears the vector
Example of using Vector in Java
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
// Creating a Vector to store Strings
Vector<String> languages = new Vector<>();
// Adding elements to the Vector
languages.add(“Java”);
languages.add(“Python”);
languages.add(“C++”);
// Displaying the elements of the Vector
System.out.println(“Programming Languages: ” + languages);
// Accessing elements by index
String firstLanguage = languages.get(0);
System.out.println(“First Language: ” + firstLanguage);
// Removing an element
languages.remove(“Python”);
System.out.println(“After removal: ” + languages);
// Checking size and capacity
System.out.println(“Size of vector: ” + languages.size());
System.out.println(“Capacity of vector: ” + languages.capacity());
}
}
When to Use Vector
Whereas the Vector class is still part of the Java Collections Framework, its utilization has decreased in favor of more modern options like ArrayList. The most reasons are:
1.Performance:
- Due to its thread-safety highlights, Vector may not be as performant as ArrayList in non-concurrent applications.
2.Thread-Safety:
- In most cases, utilizing external synchronization (e.g., Collections.synchronizedList()) or other thread-safe collections like CopyOnWriteArrayList is more effective than utilizing Vector.
That said, Vector is still valuable when working in environments where thread security may be a need and execution considerations can be secondary
