Java Utility Library

Java.util.Vector Class



Java Vector Class

Java.util package provides a vector class which models and implements vector data structure. A vector is a sequence container implementing array that can change in size. Unlike array, the size of a vector changes automatically when elements are appended or deleted. It is similar to ArrayList, but with two differences:

  • Vector is synchronized.
  • Vector contains many legacy methods that are not part of the collections framework.

Class declaration

The declaration of java.util.Vector class is:

public class Vector<E>
  extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, Serializable

Here, E is the type of element maintained by the container.

Class Constructors

S.NConstructors & Description
1. Vector()
Creates an empty vector with internal data array of size 10 and standard capacity increment 0.
2. Vector(Collection<? extends E> c)
Creates a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.
3. Vector(int initialCapacity)
Creates an empty vector with the specified initial capacity and capacity increment 0.
4. Vector(int initialCapacity, int capacityIncrement)
Creates an empty vector with the specified initial capacity and capacity increment.

java.util.Vector Methods

The java.util.Vector class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. boolean add(E element)
Adds a new element at the end of the vector.
2. void add(int index, E element)
Inserts the specified element at the specified position in the vector.
3. boolean addAll(Collection<? extends E> c)
Appends all elements of the specified collection at the end of the vector.
4. boolean addAll(int index, Collection<? extends E> c)
Inserts all elements of the specified collection at the specified position in the vector.
5. void addElement (E element)
Adds a new element at the end of the vector.
6. int capacity()
Returns current capacity of the vector.
7. void clear()
Clears all elements of the vector.
8. Object clone()
Returns a clone of the vector.
9. boolean contains(Object obj)
Returns true if the vector contains the specified element.
10. boolean containsAll(Collection<?> c)
Returns true if the vector contains all of the elements in the specified Collection.
11. void copyInto(Object[] anArray)
Copies the components of the vector into the specified array.
12. E elementAt(int index)
Returns element at the specified index in the vector.
13. Enumeration<E> elements()
Returns an enumeration of the components of the vector.
14. void ensureCapacity(int minCapacity)
Increases the capacity of the vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
15. boolean equals(Object obj)
Compares the specified Object with the Vector for equality.
16. E firstElement()
Returns the first element (element at index = 0) of the vector.
17. void forEach(Consumer<? super E> action)
Performs the given action for each element of the iterable until all elements have been processed or the action throws an exception.
18. E get(int index)
Returns the element at specified index of the vector.
19. int hashCode()
Returns the hash code value for the Vector.
20. int indexOf(Object obj)
Returns the index of the first occurrence of the specified element in the vector, or -1 if element is not present in the vector.
21. int indexOf(Object obj, int index)
Returns the index of the first occurrence of the specified element in the vector, searching forward from specified index, or -1 if element is not present in the vector.
22. void insertElementAt(E obj, int index)
Inserts the specified object as a component in the vector at the specified index.
23. boolean isEmpty()
Returns true if the vector is empty, else returns false.
24. Iterator<E> iterator()
Returns an iterator over the elements in the list in proper sequence.
25. E lastElement()
Returns the last element (element at index = size() - 1)of the vector.
26. int lastIndexOf(Object obj)
Returns the index of the last occurrence of the specified element in the vector, or -1 if element is not present in the vector.
27. int lastIndexOf(Object obj, int index)
Returns the index of the last occurrence of the specified element in the vector, searching backward from specified index, or -1 if element is not present in the vector.
28. ListIterator<E> listIterator()
Returns a list iterator over the elements in the list (in proper sequence).
29. ListIterator<E> listIterator(int index)
Returns a list iterator over the elements in the list (in proper sequence), starting at the specified position in the list.
30. E remove(int index)
Removes the element at the specified position in the vector.
31. boolean remove(Object obj)
Removes the first occurrence of the specified element from the vector, if it is present.
32. boolean removeAll(Collection<?> c)
Removes from the vector all of its elements that are contained in the specified collection.
33. void removeAllElements()
Removes all components from the vector and sets its size to zero.
34. boolean removeElement(Object obj)
Removes the first occurrence of the argument from the vector.
35. void removeElementAt(int index)
Deletes the element at the specified position in the vector.
36. boolean removeIf(Predicate<? super E> filter)
Removes all of the elements of the collection that satisfy the given predicate.
37. protected void removeRange(int fromIndex, int toIndex)
Removes from the list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
38. void replaceAll(UnaryOperator<E> operator)
Replaces each element of the list with the result of applying the operator to that element.
39. boolean retainAll(Collection<?> c)
Retains only the elements in the vector that are contained in the specified collection.
40. E set(int index, E element)
Replaces the element at the specified index in the vector with the specified element.
41. void setElementAt(E obj, int index)
Sets the component at the specified index of the vector to be the specified object.
42. void setSize(int newSize)
Sets the size of the vector.
43. int size()
Returns the number of elements in the vector.
44. void sort(Comparator<? super E> c)
Sorts the elements of the list according to the order induced by the specified comparator.
45. Spliterator<E> spliterator()
Creates a late-binding and fail-fast spliterator over the elements in the list.
46. List<E> subList(int fromIndex, int toIndex)
Returns a view of the portion of the list between fromIndex, inclusive, and toIndex, exclusive.
47. Object[] toArray()
Returns an array containing all of the elements in the vector in the correct order.
48. <T> T[] toArray(T[] a)
Returns an array containing all of the elements in the vector in the correct order; the runtime type of the returned array is that of the specified array.
49. String toString()
Returns a string representation of the vector, containing the String representation of each element.
50. void trimToSize()
Trims the capacity of the vector to be the vector's current size.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object
  • java.util.AbstractCollection<E>
  • java.util.AbstractList<E>