Vector class in java
In java, the package java.util contains a class called Vector which implements the List interface.
The Vector is similar to an ArrayList. Like ArrayList Vector also maintains the insertion order. But Vector is synchronized, due to this reason, it is rarly used in the non-thread application. It also lead to poor performance.
🔔 The Vector is a class in the java.util package.
🔔 The Vector implements List interface.
🔔 The Vector is a legacy class.
🔔 The Vector is synchronized.
The Vector class in java has the following constructor.
S. No. | Constructor with Description |
---|---|
1 |
Vector( )
It creates an empty Vector with default initail capacity of 10. |
2 |
Vector(int initialSize)
It creates an empty Vector with specified initail capacity. |
3 |
Vector(int initialSize, int incr)
It creates a vector whose initial capacity is specified by size and whose increment is specified by incr. |
4 |
Vector(Collection c)
It creates a vector that contains the elements of collection c. |
The Vector class in java has the following methods.
S.No. | Methods with Description |
---|---|
1 |
boolean add(Object o)
It appends the specified element to the end of this Vector. |
2 |
void add(int index, Object element)
It inserts the specified element at the specified position in this Vector. |
3 |
void addElement(Object obj)
Adds the specified object to the end of the vector, increasing its size by one. |
4 |
boolean addAll(Collection c)
It appends all of the elements in the specified Collection to the end of the Vector. |
5 |
boolean addAll(int index, Collection c)
It inserts all of the elements in in the specified Collection into the Vector at the specified position. |
6 |
Object set(int index, Object element)
It replaces the element at the specified position in the vector with the specified element. |
7 |
void setElementAt(Object obj, int index)
It sets the element at the specified index of the vector to be the specified object. |
8 |
Object remove(int index)
It removes the element at the specified position in the vector. |
9 |
boolean remove(Object o)
It removes the first occurrence of the specified element in the vector. |
10 |
boolean removeElement(Object obj)
It removes the first occurrence of the specified element in the vector. |
11 |
void removeElementAt(int index)
It removes the element at specified index in the vector. |
12 |
void removeRange(int fromIndex, int toIndex)
It removes from the Vector all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive. |
13 |
boolean removeAll(Collection c)
It removes from the vector all of its elements that are contained in the specified Collection. |
14 |
void removeAllElements()
It removes all the elements from the vector. |
15 |
boolean retainAll(Collection c)
It removes all the elements from the vector except elements those are in the given collection. |
16 |
Object elementAt(int index)
It returns the element at specified index in the Vector. |
17 |
Object get(int index)
It returns the element at specified index in the Vector. |
18 |
Enumeration elements()
It returns the Enumeration of all the elements of the Vector. |
19 |
Object firstElement()
It returns the first element of the Vector. |
20 |
Object lastElement()
It returns the last element of the Vector. |
21 |
int indexOf(Object element)
It returns the index value of the first occurence of the given element in the Vector. |
22 |
int indexOf(Object elem, int index)
It returns the index value of the first occurence of the given element, search beginning at specified index in the Vector. |
23 |
int lastIndexOf(Object elememnt)
It returns the index value of the last occurence of the given element, search beginning at specified index in the Vector. |
24 |
List subList(int fromIndex, int toIndex)
It returns a list containing elements fromIndex to toIndex in the Vector. |
25 |
int capacity()
It returns the current capacity of the Vector. |
26 |
void clear()
It removes all the elements from the Vector. |
27 |
Object clone()
It returns a clone of the Vector. |
28 |
boolean contains(Object element)
It returns true if element found in the Vector, otherwise returns false. |
29 |
boolean containsAll(Collection c)
It returns true if all the elements of geven collection found in the Vector, otherwise returns false. |
30 |
void ensureCapacity(int minCapacity)
It increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument. |
31 |
boolean equals(Object o)
It compares the specified Object with this vector for equality. |
32 |
int hashCode()
It returns the hash code of the Vector. |
33 |
boolean isEmpty()
It returns true if Vector has no elements, otherwise returns false. |
34 |
void setSize(int newSize)
It sets the size of the vector. |
35 |
int size()
It returns total number of elements in the vector. |
36 |
Object[] toArray()
It returns an array containing all the elements of the Vector. |
37 |
String toString()
It returns a string representation of the Vector. |
38 |
void trimToSize()
It trims the capacity of the vector to be the vector's current size. |
Let's consider an example program to illustrate methods of Vector class.
import java.util.*;
public class VectorClassExample {
public static void main(String[] args) {
Vector list = new Vector();
list.add(10);
list.add(30);
list.add(0, 100);
list.addElement(50);
System.out.println("Vector => " + list);
System.out.println("get(2) => " + list.get(2));
System.out.println("firstElement() => " + list.firstElement());
System.out.println("indexOf(50) => " + list.indexOf(50));
System.out.println("contains(30) => " + list.contains(30));
System.out.println("capacity() => " + list.capacity());
System.out.println("size() => " + list.size());
System.out.println("isEmpty() => " + list.isEmpty());
}
}
When we execute the above code, it produce the following output.