Vector - Notes By ShariqSP
Vector in Java
Vector
is a resizable array implementation of the List
interface in Java. It is similar to ArrayList
but provides synchronized operations, making it thread-safe.
Definition:
A Vector
is a collection that maintains an underlying array to store elements. It dynamically grows and shrinks as elements are added or removed, similar to an ArrayList
. However, unlike ArrayList
, Vector
is synchronized, meaning it is thread-safe for use in multi-threaded environments.
Methods:
- add(E element): Adds the specified element to the end of the vector.
- add(int index, E element): Inserts the specified element at the specified position in the vector.
- remove(int index): Removes the element at the specified position in the vector.
- get(int index): Returns the element at the specified position in the vector.
- size(): Returns the number of elements in the vector.
- isEmpty(): Returns true if the vector contains no elements.
- contains(Object obj): Returns true if the vector contains the specified element.
- clear(): Removes all elements from the vector.
Example:
import java.util.Vector;
public class Main {
public static void main(String[] args) {
// Create a Vector of integers
Vector vector = new Vector<>();
// Add elements to the vector
vector.add(10);
vector.add(20);
vector.add(30);
// Display the elements
System.out.println("Elements in the vector:");
System.out.println(vector);
// Remove an element
vector.remove(1);
// Display the modified vector
System.out.println("\nElements after removal:");
System.out.println(vector);
}
}
In this example, a Vector
is created to store integers. Elements are added to the vector using the add()
method and removed using the remove()
method.
Advantages:
- Synchronization: Vector is synchronized by default, meaning it is thread-safe for concurrent access by multiple threads. This makes Vector suitable for scenarios where thread safety is required without the need for external synchronization mechanisms.
- Dynamic Sizing: Vector provides dynamic sizing, allowing for the addition and removal of elements at runtime without needing to specify the size beforehand. This flexibility makes Vector suitable for scenarios where the size of the collection may vary.
- Random Access: Vector provides efficient random access to elements by index. This means that elements can be quickly retrieved or modified by their position in the vector, making Vector efficient for operations that require frequent access to elements at specific positions.
- Standardized API: Vector implements the List interface, providing a standardized set of methods for adding, removing, and accessing elements. This makes Vector easy to use and interoperable with other List implementations, simplifying code maintenance and improving code readability.
Disadvantages:
- Synchronization Overhead: The synchronization provided by Vector can introduce performance overhead, especially in scenarios where synchronization is not required. If thread safety is not a concern, other List implementations like ArrayList or LinkedList may offer better performance without the synchronization overhead.
- Memory Overhead: Vector requires additional memory to maintain its internal array structure. This overhead can become significant, especially when dealing with large datasets or when the initial capacity is set too high.
- Slower Operations: While Vector provides efficient random access, insertion and deletion operations at arbitrary positions can be slower, especially when elements need to be shifted to accommodate the change in size. LinkedList may be more efficient for frequent insertion and deletion in the middle of the list.
- Not Modern: Vector is a legacy class introduced in early versions of Java. It has been largely replaced by more modern and efficient alternatives like ArrayList and LinkedList. While Vector may still be used in existing codebases for compatibility reasons, it is generally not recommended for new development.
Interview Questions and MCQs on Vector in Java
Interview Questions:
- What is a Vector in Java?
- How is Vector different from ArrayList?
- What are the key features of Vector?
- How is capacity increased in Vector?
- What are the synchronization features of Vector?
- What is the role of the synchronized keyword in Vector?
- How do you add elements to a Vector?
- How do you remove elements from a Vector?
- Explain the concept of resizing in Vector.
- What are the advantages and disadvantages of using Vector?
Multiple Choice Questions (MCQs):
- Which package is required to use Vector in Java?
a) java.util
b) java.lang
c) java.util.collection
d) java.util.vector
Answer: a) java.util - Which of the following is true about Vector?
a) It is synchronized by default
b) It is not synchronized by default
c) It can't contain null elements
d) None of the above
Answer: a) It is synchronized by default - Which method is used to add an element to the end of a Vector?
a) addLast()
b) add()
c) append()
d) addToEnd()
Answer: b) add() - Which method is used to remove an element from a Vector at a specific index?
a) remove(index)
b) removeElement(index)
c) delete(index)
d) removeAt(index)
Answer: a) remove(index) - Which interface does Vector implement?
a) List
b) Collection
c) Iterable
d) All of the above
Answer: d) All of the above