Collection Framework - Vector Class

Similar to ArrayList, Vector also implements dynamic arrays. The difference being Vector is synchronized.

Vector is a legacy class. But with the introduction of collection and later JDK 5, Vector was reimplemented to support generics and implement the List interface.

Vector also is initialized with an initial capacity of 10. It also operates same as ArrayList. Once the initial capacity is filled, a new Vector is created and all the elements are copied to the new object. The distinction being the new object created has twice the capacity of the original object. In case of arraylist, it used to be 50% more then the original object.

Vector class comes with the following constructors :

Vector() - default constructor that will have an initial capacity of 10.
Vector(int initialCapacity) - creates a Vector object with the specified capacity
Vector(int initialCapacity, int increment) - creates a Vector object with the specified capacity and once that capacity is exhausted, it increments the size by the given increment value
Vector(Collection coll) -create vector from a collection

The example with the implementation of Vector can be found here

 In addition, the example also shows the Enumeration interface can be used to iterate through the vector.  Enumeration interface is again a legacy interface.

Apart from the methods of the List interface, Vector also comes with legacy methods for adding and removing elements like addElement(), addElementAt(), removeElement(), removeAllElements(), etc.

Comments

Popular posts from this blog

Collection Framework - HashSet And LinkedHashSet class

Collection Framework - Cursors in Java

Hashed data structures