Vector is a legacy collection class in Java that is similar to ArrayList, but with some key differences. Vector is a synchronized collection class, which means it is thread-safe and can be used in multi-threaded applications without causing concurrency issues. However, this comes at a cost of performance, as synchronization can cause some overhead.
The Vector class has several constructors that allow you to create a Vector object. Here are some of the constructors:
1. Vector(): This constructor creates an empty Vector with an initial capacity of 10.
2. Vector(int initialCapacity): This constructor creates an empty Vector with the specified initial capacity.
3. Vector(int initialCapacity, int capacityIncrement): This constructor creates an empty Vector with the specified initial capacity and capacity increment.
4. Vector(Collection<? extends E> c): This constructor creates a Vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.
Vector provides several methods to add, remove, retrieve, and manipulate elements in the list. It also provides methods to search, sort, and perform other operations on the elements of the list. The methods of Vector are similar to those of ArrayList.
Here are some of the methods of the Vector class in Java:
1. add(E element): This method is used to add an element to the end of the Vector.
2. add(int index, E element): This method is used to add an element at a specific index in the Vector.
3. remove(Object o): This method is used to remove the first occurrence of the specified element from the Vector.
4. remove(int index): This method is used to remove the element at the specified index from the Vector.
5. get(int index): This method is used to retrieve the element at the specified index from the Vector.
6. set(int index, E element): This method is used to replace the element at the specified index with the specified element.
7. size(): This method is used to return the number of elements in the Vector.
8. indexOf(Object o): This method is used to return the index of the first occurrence of the specified element in the Vector.
9. clear(): This method is used to remove all the elements from the Vector.
10. sort(Comparator<? super E> c): This method is used to sort the elements of the Vector using the specified Comparator.
Here's an example that demonstrates how to use Vector and its methods in Java:
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
// Creating a Vector
Vector<String> vector = new Vector<>();
// Adding elements to the Vector using add() method
vector.add("apple");
vector.add("banana");
vector.add("cherry");
vector.add("date");
vector.add("elderberry");
// Printing the Vector
System.out.println("Original Vector: " + vector);
// Adding an element at a specific index using add() method
vector.add(2, "grape");
// Printing the updated Vector
System.out.println("Vector after adding 'grape' at index 2: " + vector);
// Removing an element using remove() method
vector.remove("date");
// Printing the updated Vector
System.out.println("Vector after removing 'date': " + vector);
// Retrieving an element using get() method
String fruit = vector.get(3);
System.out.println("Element at index 3: " + fruit);
// Replacing an element using set() method
vector.set(4, "fig");
// Printing the updated Vector
System.out.println("Vector after replacing 'elderberry' with 'fig': " + vector);
// Sorting the Vector using sort() method
vector.sort(null);
// Printing the sorted Vector
System.out.println("Sorted Vector: " + vector);
// Removing all elements using clear() method
vector.clear();
// Printing the empty Vector
System.out.println("Vector after clearing all elements: " + vector);
}
}
Output:
Original Vector: [apple, banana, cherry, date, elderberry]
Vector after adding 'grape' at index 2: [apple, banana, grape, cherry, date, elderberry]
Vector after removing 'date': [apple, banana, grape, cherry, elderberry]
Element at index 3: cherry
Vector after replacing 'elderberry' with 'fig': [apple, banana, grape, cherry, fig]
Sorted Vector: [apple, banana, cherry, fig, grape]
Vector after clearing all elements: []
In this example, we created a Vector and added elements to it using the add() method. We also demonstrated how to add an element at a specific index using add() method, remove an element using remove() method, retrieve an element using get() method, and replace an element using set() method.
We also showed how to sort the Vector using sort() method and clear all the elements from the Vector using clear() method.
Overall, the Vector class provides a thread-safe way to manipulate a list of elements in Java, although its performance may be impacted by synchronization.