Java Vector tutorial with examples will help you understand how to use Vector class in an easy way. The Vector in Java is an implementation of the List interface that grows automatically as we add elements to it.
The Vector class is contained in the java.util package so we have to import the java.util package using the import statement to use the Vector class in the code.
The Vector class was a part of Java 1.0, but in Java 2, the Vector class was rewritten to implement the List interface and thus it became the part of the Java Collection Framework.
Unlike the ArrayList class in Java, the Vector is synchronized but it comes with a cost. If the application does not need thread safety, it is recommended to use the ArrayList instead of the Vector class to get better performance.
Like the ArrayList, the Vector class is also a dynamic array of objects. It maintains an internal array to store its elements and the size of this array is increased or decreased automatically as needed. The size of this internal array is called the capacity of the Vector.
The size of the internal array is increased automatically by a certain number when the vector size becomes greater than the internal array size. This certain number is called the capacity increment of the Vector.
The Vector elements can be accessed using the index just like an array. The index of the elements starts from 0 and ends at the size of the vector – 1 index.
How to create new objects of the Java Vector class?
The Vector class in Java provides several constructors using which we can create new objects of it. The default constructor creates a new and empty vector object.
1 |
public Vector() |
The object created this way has a capacity of 10 and a capacity increment of 0.
If you want to specify the initial capacity and capacity increment of the Vector object at the creation time, you can use any of the below given overloaded constructors.
1 |
public Vector(int initialCapacity) |
1 |
public Vector(int initialCapacity, int capacityIncrement) |
The Vector class also provides an overloaded constructor that accepts a Collection object as an argument.
1 |
public Vector(Collection<? extends E> collection) |
It creates a new vector object containing all the elements of the specified collection object. The vector object’s capacity, in this case, will be large enough to hold all the elements of the collection.
Java Vector Methods
The below given are some of the important methods of the Vector class in Java.
How to add elements to Vector using the add, addAll, and addElement methods?
The Vector add
method adds the specified element at the end of the vector.
1 |
public boolean add(E element) |
It appends the element at the end and returns true.
1 2 3 4 5 6 7 8 9 |
Vector<Integer> vector = new Vector<Integer>(); //this will add element 1 at the end and return true System.out.println( vector.add(1) ); //this will add element 2 at the end and return true System.out.println( vector.add(2) ); System.out.println("Vector contains: " + vector); |
Output
1 2 3 |
true true Vector contains: [1, 2] |
The Vector addElement
method is similar to the add
method and appends the element at the end.
1 |
public void addElement(E object) |
How to insert an element in the Vector at the specified index?
The above given add
method appends the given element in the vector object. If you want to insert an element at the specified index, use below given overloaded add
method.
1 |
public void add(int index, E element) |
It inserts the specified element at the specified index in the vector object and shifts any subsequent elements to the right by adding 1 to their indices.
1 2 3 4 5 6 7 8 9 10 11 12 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(1); vector.add(2); vector.add(3); vector.add(4); vector.add(5); //this will insert element 10 at index 1 vector.add(1, 10); System.out.println("Vector contains: " + vector); |
Output
1 |
Vector contains: [1, 10, 2, 3, 4, 5] |
We can also use the insertElementAt
method that is identical to the above given add
method in functionality.
1 |
public void insertElementAt(E object, int index) |
Notice the difference in the parameter order between these two methods.
How to add all elements of a Collection to the Vector using the addAll method?
The Vector addAll
method appends all elements of the specified collection to this vector object.
1 |
public boolean addAll(Collection<? extends E> collection) |
It returns true if the vector object was changed as a result of this method call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); Vector<Integer> vector = new Vector<Integer>(); vector.add(4); vector.add(5); //this will append all elements of ArrayList to Vector vector.addAll(list); System.out.println("Vector contains: " + vector); |
Output
1 |
Vector contains: [4, 5, 1, 2, 3] |
If you want to insert the collection elements at the specific index instead of at the end, you can use below given overloaded addAll
method.
1 |
public boolean addAll(int index, Collection<? extends E> collection) |
It inserts all the collection elements at the given index in vector and shifts the subsequent elements to the right.
How to get elements from the Vector?
How to get an element at the specific Vector index using the get and elementAt methods?
The Vector get
method returns an element located at the specified index.
1 |
public E get(int index) |
1 2 3 4 5 6 7 8 9 10 11 12 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(32); vector.add(21); vector.add(23); vector.add(16); //this will return element at index 1 i.e. 21 System.out.println( vector.get(1) ); //this will return element at index 3 i.e. 16 System.out.println( vector.get(3) ); |
Output
1 2 |
21 16 |
The Vector elementAt
method is identical to the get
method in functionality.
1 |
public E elementAt(int index) |
Both of these methods throw ArrayIndexOutOfBoundsException if the specified index is out of the range.
How to get the first and last elements from the Vector?
The Vector firstElement
method returns the first element of the Vector while the lastElement
method returns the last.
1 |
public E firstElement() |
1 |
public E lastElement() |
Both of these methods throw NoSuchElementException if the vector object is empty. Make sure to check the size of the Vector before calling these methods to avoid this exception.
1 2 3 4 5 6 7 8 9 10 11 12 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(1); vector.add(2); vector.add(3); vector.add(4); //this will return the first element i.e. 1 System.out.println( vector.firstElement() ); //this will return the last element i.e. 4 System.out.println( vector.lastElement() ); |
Output
1 2 |
1 4 |
How to replace an element at the specific index of the Vector using the set and setElementAt methods?
The Vector set
method replaces an old element with the given new element at the specified index of the vector object.
1 |
public E set(int index, E element) |
It returns the old element that was replaced.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(1); vector.add(2); vector.add(3); vector.add(4); /* * this will replace 1 i.e. element at index 0 * with 10 and return 1 */ System.out.println( vector.set(0, 10) ); System.out.println("Vector contains: " + vector); |
Output
1 2 |
1 Vector contains: [10, 2, 3, 4] |
The Vector setElementAt
method is identical to the set
method in functionality.
1 |
public void setElementAt(E object, int index) |
Please note the change in the order of the parameters. Plus, the set
method returns the previous element located at the specified index, while the return type of the setElementAt
method is void.
How to get the size of the Vector using the size method?
The Vector size
method returns the number of elements stored in the vector object.
1 |
public int size() |
1 2 3 4 5 6 7 8 9 10 |
Vector<Integer> vector = new Vector<Integer>(); //this will return 0 System.out.println(vector.size()); vector.add(1); vector.add(2); //this will return 2 System.out.println(vector.size()); |
Output
1 2 |
0 2 |
How to set the size of the Vector using the setSize method?
The Vector setSize
method sets the size of this vector object.
1 |
public void setSize(int newSize) |
If the specified new size is greater than the current size of this vector object, null elements are added at the end of this vector object. If the specified new size is less than the size of the vector object, elements having an index greater than or equal to the new size are discarded.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(11); vector.add(22); vector.add(33); vector.add(44); vector.add(55); /* * this will append 2 null elements at the end * of the vector to make the size of 7 from 5 */ vector.setSize(7); System.out.println("Vector size is: " + vector.size()); System.out.println("Vector contains: " + vector); /* * this will discard all elements having * index 2 or greater to decrease the * vector size from 7 to 2 */ vector.setSize(2); System.out.println("Vector size is: " + vector.size()); System.out.println("Vector contains: " + vector); |
Output
1 2 3 4 |
Vector size is: 7 Vector contains: [11, 22, 33, 44, 55, null, null] Vector size is: 2 Vector contains: [11, 22] |
Tip: We can use the setSize
method to remove all elements from the vector by passing 0 to it.
How to check if the Vector is empty using the isEmpty method?
The Vector isEmpty
method returns true if there are no elements in this vector object.
1 |
public boolean isEmpty() |
It returns false if there is at least one element in the vector object.
1 2 3 4 5 6 7 8 9 10 |
Vector<Integer> vector = new Vector<Integer>(); //this will return true as there are no elements System.out.println(vector.isEmpty()); vector.add(1); vector.add(2); //this will return false as there are 2 elements System.out.println(vector.isEmpty()); |
Output
1 2 |
true false |
How to remove elements from the Vector?
How to remove an element from a specific index of Vector using the remove and removeElementAt methods?
The Vector remove
method removes the element located at the specified index from the vector object.
1 |
public E remove(int index) |
It removes the element from the specified index of the vector, shifts the subsequent elements to the left and returns the removed element.
1 2 3 4 5 6 7 8 9 10 11 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(1); vector.add(2); vector.add(3); vector.add(4); //this will remove element at index 0 i.e. 1 and return 1 System.out.println( vector.remove(0) ); System.out.println("Vector contains: " + vector); |
Output
1 2 |
1 Vector contains: [2, 3, 4] |
The removeElementAt
method is identical to this method in functionality.
1 |
public void removeElementAt(int index) |
However, the return type of this method is void while the remove
method returns the element that was removed.
Both of these methods throw ArrayIndexOutOfBoundsException exception if the specified index is out of the range.
How to remove a specific element object from Vector using the remove and removeElement methods?
The overloaded remove
method removes the specified element from the vector.
1 |
public boolean remove(Object object) |
It removes the first occurrence of the specified object from the vector, shifts the subsequent elements to the left and returns true if the element is found. It returns false if the element is not found in the vector.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Vector<String> vColors = new Vector<String>(); vColors.add("red"); vColors.add("green"); vColors.add("blue"); //this will remove "green" and return true System.out.println( vColors.remove("green") ); //this will return false as element "yellow" does not exist System.out.println( vColors.remove("yellow") ); System.out.println("Vector contains: " + vColors); |
Output
1 2 3 |
true false Vector contains: [red, blue] |
The removeElement
method is identical to this method in functionality.
1 |
public boolean removeElement(Object object) |
Note: If the Vector contains the objects of a custom class, then the custom class must override the equals
and hashCode
methods for these methods to find and remove elements.
How to clear or remove all elements from Vector using the clear and removeAllElements methods?
The Vector clear
method removes all elements from the vector object.
1 |
public void clear() |
The vector object becomes empty after this method call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(1); vector.add(2); vector.add(3); vector.add(4); //this will return false System.out.println(vector.isEmpty()); /* * To remove all elements from the vector, use * the clear method */ vector.clear(); //this will now return true System.out.println(vector.isEmpty()); |
Output
1 2 |
false true |
The removeAllElements
method is similar to the clear
method in functionality.
1 |
public void removeAllElements() |
How to remove all elements from Vector that are not contained in a collection using the retainAll method?
The Vector retainAll
method retains all the elements in the vector that are also contained in the specified collection object. The rest of the elements are removed from this vector object.
1 |
public boolean retainAll(Collection<?> collection) |
In other words, it is an intersection between this vector object and the specified collection object. Only matching elements between these two objects are retained in the vector, non-matching elements will be removed.
The retainAll
method returns true if this vector object is changed due to this method call, false otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
ArrayList<Integer> aList = new ArrayList<Integer>(); aList.add(1); aList.add(2); Vector<Integer> vector = new Vector<Integer>(); vector.add(1); vector.add(2); vector.add(3); //intersection of the ArrayList and Vector System.out.println( vector.retainAll(aList) ); //this will have only matching elements i.e. 1 and 2. System.out.println("Vector contains: " + vector); |
Output
1 2 |
true Vector contains: [1, 2] |
How to find the index of an element using the indexOf and lastIndexOf methods?
How to find the index of the first occurrence of the element?
The Vector indexOf
method returns the index of the first occurrence of the specified element in the vector.
1 |
public int indexOf(Object object) |
It returns -1 if the specified object element is not found in the vector.
1 2 3 4 5 6 7 8 9 10 11 |
Vector<String> vColors = new Vector<String>(); vColors.add("red"); vColors.add("green"); vColors.add("blue"); //this will return 2 i.e. index of element "blue" System.out.println( vColors.indexOf("blue") ); //this will return -1 as element "yellow" does not exist System.out.println( vColors.indexOf("yellow") ); |
Output
1 2 |
2 -1 |
The above given indexOf
method starts searching for the specified element from index 0. If you want to search for the element from the specified index instead of the 0, you can use the below given overloaded indexOf
method.
1 |
public int indexOf(Object object, int index) |
It will return an index of the first occurrence of the specified object element from the given index.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Vector<String> vColors = new Vector<String>(); vColors.add("red"); vColors.add("green"); vColors.add("blue"); vColors.add("red"); vColors.add("green"); vColors.add("blue"); /* * this will return 1 i.e. index of the first occurrence * of the element "green" starting from index 0 */ System.out.println( vColors.indexOf("green") ); /* * this will return 4 i.e. index of the first occurrence * of the element "green" starting from index 2 */ System.out.println( vColors.indexOf("green", 2) ); |
Output
1 2 |
1 4 |
This method throws IndexOutOfBoundsException if the specified index is negative.
Note: If the Vector contains the objects of a custom class, then the custom class must override the equals
and hashCode
methods for these methods to work.
How to find the index of the last occurrence of the element?
The Vector lastIndexOf
method returns an index of the last occurrence of the specified element in the vector object.
1 |
public int lastIndexOf(Object object) |
It returns -1 if the specified object element is not found in the vector.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Vector<String> vColors = new Vector<String>(); vColors.add("red"); vColors.add("green"); vColors.add("blue"); vColors.add("red"); vColors.add("green"); vColors.add("blue"); //this will return 4 i.e. last index of the element "green" System.out.println( vColors.lastIndexOf("green") ); //this will return -1 as the element "yellow" does not exist System.out.println( vColors.lastIndexOf("yellow") ); |
Output
1 2 |
4 -1 |
If you want to search for the last occurrence of the element from the given index, use the overloaded lastIndexOf
method.
1 |
public int lastIndexOf(Object object, int index) |
This method searches for the last occurrence of the specified element starting from the given index searching in a backward direction.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Vector<String> vColors = new Vector<String>(); vColors.add("red"); vColors.add("green"); vColors.add("blue"); vColors.add("red"); vColors.add("green"); vColors.add("blue"); /* * This will return 4 i.e. index of last occurrence * of "green" searching backwards from index 5 */ System.out.println( vColors.lastIndexOf("green", 5) ); /* * This will return 1 i.e. index of last occurrence * of "green" searching backwards from index 3 */ System.out.println( vColors.lastIndexOf("green", 3) ); /* * This will return -1 because element blue does not exist * before index 1 searching backwards */ System.out.println( vColors.lastIndexOf("blue", 1) ); |
Output
1 2 3 |
4 1 -1 |
This method throws IndexOutOfBoundsException if the specified index greater than or equal to the size of the vector.
Note: If the Vector contains the objects of a custom class, then the custom class must override the equals
and hashCode
methods for these methods to work.
How to check if element exists in Vector using the contains method?
The Vector contains
method returns true if the specified element exists in this vector object.
1 |
public boolean contains(Object object) |
It returns false if the vector does not contain the specified element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Vector<String> vColors = new Vector<String>(); vColors.add("red"); vColors.add("green"); vColors.add("blue"); /* * To check if the element exits in the * vector object, use the contains method */ //this will return true as vector contains element "red" System.out.println( vColors.contains("red") ); //this will return false as vector dose not contain "yellow" System.out.println( vColors.contains("yellow") ); |
Output
1 2 |
true false |
Note: If the Vector contains the objects of a custom class, then the custom class must override the equals
and hashCode
methods for the contains
method to work properly.
How to check if the Vector contains all elements of a collection using the containsAll method?
The Vector containsAll
method returns true if all elements of the specified collection are also present in this vector object.
1 |
public boolean containsAll(Collection<?> collection) |
It returns true if this vector object contains all the elements of the specified collection object, false otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
ArrayList<Integer> aList = new ArrayList<Integer>(); aList.add(1); aList.add(2); Vector<Integer> vector = new Vector<Integer>(); vector.add(1); vector.add(2); vector.add(3); /* * This will return true because all elements * of the ArrayList are also contained in * this vector object */ System.out.println( vector.containsAll(aList) ); //add an element in the ArrayList aList.add(5); //this will now return false, as 5 does not exist in the vector System.out.println( vector.containsAll(aList) ); |
Output
1 2 |
true false |
How to iterate Vector?
There are several ways using which we can iterate over vector elements as given below.
Using Enumeration
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(11); vector.add(22); vector.add(33); //Get an enumeration using the elements method Enumeration<Integer> enumeration = vector.elements(); //use hasMoreElements and nextElement methods to iterate while( enumeration.hasMoreElements() ){ System.out.println( enumeration.nextElement() ); } |
Output
1 2 3 |
11 22 33 |
Using Iterator
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(11); vector.add(22); vector.add(33); //get an iterator using the iterator method Iterator<Integer> itr = vector.iterator(); //iterate using the hasNext and next methods while( itr.hasNext() ){ System.out.println( itr.next() ); } |
Output
1 2 3 |
11 22 33 |
Using ListIterator
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(11); vector.add(22); vector.add(33); //get list iterator using the listIterator method ListIterator<Integer> listIterator = vector.listIterator(); //iterate using hasNext and next methods while( listIterator.hasNext() ){ System.out.println( listIterator.next() ); } |
Output
1 2 3 |
11 22 33 |
Using for loop
1 2 3 4 5 6 7 8 9 10 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(11); vector.add(22); vector.add(33); //iterate using the for loop for(int i = 0 ; i < vector.size() ; i++){ System.out.println( vector.get(i) ); } |
Output
1 2 3 |
11 22 33 |
Using enhanced for loop
1 2 3 4 5 6 7 8 9 10 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(11); vector.add(22); vector.add(33); //iterate using enhanced for loop for(Integer element : vector){ System.out.println( element ); } |
Output
1 2 3 |
11 22 33 |
Using forEach (Java version 8 and later)
1 2 3 4 5 6 7 8 9 10 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(11); vector.add(22); vector.add(33); //iterate using Java 8 forEach vector.forEach( element -> { System.out.println( element ); }); |
Output
1 2 3 |
11 22 33 |
How to iterate Vector in reverse direction (backward direction)?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(11); vector.add(22); vector.add(33); /* * get list iterator using the listIterator method and * position it at the end of the vector */ ListIterator<Integer> listIterator = vector.listIterator( vector.size() ); //iterate in reverse direction using hasPrevious and previous methods while( listIterator.hasPrevious() ){ System.out.println( listIterator.previous() ); } |
Output
1 2 3 |
33 22 11 |
How to get a sublist from the Vector using the subList method?
The Vector subList
method returns a view of part of this vector object whose elements are between the specified start and end index.
1 |
public List<E> subList(int fromIndex, int toIndex) |
The fromIndex is inclusive while the toIndex is exclusive.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(11); vector.add(22); vector.add(33); vector.add(44); vector.add(55); /* * This will return a sublist containing * elements at index 1, 2, and 3. * * The start index is inclusive while the * end index is exclusive. */ List<Integer> sublist = vector.subList(1, 4); System.out.println("Sublist contains: " + sublist); |
Output
1 |
Sublist contains: [22, 33, 44] |
The sublist returned from this method is backed by the original vector object so any changes you make to the sublist will also be reflected in the original vector object, and vice versa.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(11); vector.add(22); vector.add(33); vector.add(44); vector.add(55); List<Integer> sublist = vector.subList(1, 4); //add element to sublist sublist.add(600); System.out.println("Sublist contains: " + sublist); System.out.println("Vector contains: " + vector); |
Output
1 2 |
Sublist contains: [22, 33, 44, 600] Vector contains: [11, 22, 33, 44, 600, 55] |
The subList
method throws IndexOutOfBoundsException if the start index is less than 0 or the end index is greater than the vector size. It also throws IllegalArgumentException if the end index is greater than the start index.
How to convert a Vector to an array using the toArray method?
The Vector toArray
method returns an array containing all the elements of the vector.
1 |
public <T> T[] toArray(T[] a) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Vector<Integer> vector = new Vector<Integer>(); vector.add(11); vector.add(22); vector.add(33); vector.add(44); vector.add(55); //create new array of the same type and size Integer[] array = new Integer[ vector.size() ]; //use the toArray method to convert vector to array array = vector.toArray(array); System.out.println("Array contains: " + Arrays.toString(array)); |
Output
1 |
Array contains: [11, 22, 33, 44, 55] |
Tip: If the array specified in the toArray
method is large enough to hold all the vector elements, the same array is filled with the vector elements and returned. The array element immediately after the vector elements is set to null to indicate the end of the vector elements.
If the specified array is smaller than the size of the vector, then a new array is allocated, filled with vector elements, and returned.
Always try to specify the array of at least the same size as the vector to avoid the allocation of a new array.
How to clone a Vector object using the clone method?
The Vector clone
method returns a shallow copy of this vector object.
1 |
public Object clone() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(1); vNumbers.add(2); vNumbers.add(3); /* * To clone a vector object, use the * clone method. */ Vector<Integer> vNumbersClone = (Vector<Integer>) vNumbers.clone(); System.out.println("Original vector: " + vNumbers); System.out.println("Cloned vector: " + vNumbersClone); |
Output
1 2 |
Original vector: [1, 2, 3] Cloned vector: [1, 2, 3] |
Please note that the clone
method copies only references of the vector elements, not the actual objects. The element references in the cloned vector object still refer to the same element objects.
To understand this, let’s see an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
class Student{ private int id; private String name; public Student(int id, String name){ this.id = id; this.name = name; } public void setName(String name){ this.name = name; } public String toString(){ return "[" + this.id + "->" + this.name + "]"; } } public class VectorTutorial { public static void main(String[] args) { Vector<Student> vStudents = new Vector<Student>(); Student s1 = new Student(1, "Roy"); Student s2 = new Student(2, "Jason"); vStudents.add(s1); vStudents.add(s2); Vector<Student> vStudentsCloned = (Vector<Student>) vStudents.clone(); System.out.println("Original: " + vStudents); System.out.println("Cloned: " + vStudentsCloned); //change the element object s1.setName("NewName"); System.out.println("Original: " + vStudents); System.out.println("Cloned: " + vStudentsCloned); } } |
Output
1 2 3 4 |
Original: [[1->Roy], [2->Jason]] Cloned: [[1->Roy], [2->Jason]] Original: [[1->NewName], [2->Jason]] Cloned: [[1->NewName], [2->Jason]] |
As we can see from the output when we change the actual element object, the change is reflected in both vector objects i.e. original as well as cloned object.
What is a Vector capacity and how to manage it?
The Vector is a dynamic array implementation that grows automatically as and when we add more elements to it. It maintains an internal array to store its elements and the size of this internal array is called the capacity of the vector.
To know the current capacity of the vector object, use the capacity
method.
1 |
public int capacity() |
The default initial capacity of the Vector object is 10. However, we can use the overloaded constructors to specify the custom initial capacity when we create objects of it.
The Vector class in Java automatically increases the size of this internal array when it is no more sufficient to hold the number of elements we want to add. It does this by allocating a new array with the bigger size and copying the existing elements to the new array. This is a costly operation in terms of performance.
Now suppose we want to add thousand of elements to the vector one by one. So the capacity of the internal array needs to be increased many times to accommodate the new elements. This could impact the performance of the application. If we know approximately how many elements we want to add to vector, we can allocate that much capacity beforehand to avoid this performance penalty.
This can be done in two ways, either at the creation time using the constructor that accepts custom capacity or by using the ensureCapacity
method.
1 |
public void ensureCapacity(int minCapacity) |
This method increases the vector capacity (i.e. the size of the internal array), if required, to hold at least the specified number of elements.
Once the vector capacity is increased, it is not reduced automatically even if you remove the elements from it. If we do not need the increased capacity anymore, we can use the trimToSize
method to reduce its capacity to free up the storage space.
1 |
public void trimToSize() |
This method reduces the capacity of the vector object to the current size of the vector object.
Below given additional Java Vector examples will help you understand Vector concepts in more detail.
Java Vector Examples
- How to create new Vector objects in Java
- How to add elements to Vector in Java
- How to replace an element at specific index of the Vector
- How to insert an element at the beginning of the Java Vector
- How to get elements from Vector (Vector get method)
- How to get first and last elements from the Vector
- How to get random elements from the Vector
- How to get the size of the Vector (Vector length)
- How to remove elements from the Vector
- How to clear or remove all elements from the Vector
- How to remove duplicate elements from the Vector
- How to remove the first element or last element from the Vector
- How to check if Vector is empty in Java (isEmpty method)
- How to check if element exists in Vector in Java
- How to find an element in the Vector using indexOf and lastIndexOf methods
- How to find the minimum or maximum element from the Vector
- How to iterate through the Vector elements
- How to iterate the Vector elements in the reverse order (backward direction)
- How to sort Vector in Java
- How to shuffle Vector elements in Java
- How to copy, clone, and append (merge) one Vector to another Vector in Java
- How to convert an array to Vector
- How to convert Vector to an array
- How to convert Vector to ArrayList and ArrayList to Vector in Java
References:
Java 8 Vector Documentation
Please let me know if you liked the Java Vector tutorial with examples in the comments section below.