Introduction to Vectors in Java : Insertion and Deletion!
Hello Learners, today we are going to learn about vectors in Java. Different operations on vectors like insertion, deletion, search operation, etc. The Vector is a class in java.util package added in JDK 1.0 before the collection interface (added in JDK 1.2). That’s why we call it a Legacy Class.
Vector internally uses a dynamic array and it is similar to ArrayList but there are some differences also which are listed below:
- Vectors are Synchronized (i.e. only one thread execution at a time).
- Vectors have some legacy methods which are not supported by the collection framework.
- The Vector class was reengineered with the release of Java 2. Vectors now extend the AbstractList class and implement the List interface which makes it fully compatible with collections now.
- Vectors have low key performance, it is slow because only one thread is executed at a time.
- The Vectors are thread-safe because of synchronization.
- Vectors can use the methods of ArrayList but ArrayList can not use the methods of vectors (or we can say the legacy methods).
Constructors of Vector Class in Java
- Vector(): the constructor having no argument creates a default vector with an initial size of 10.
- Vector(int size): this type of constructor creates a vector whose capacity is defined by integer size.
- Vector(int size, int increment): creates a vector whose capacity is defined by size and increment factor is specified by increment.
- Vector(Collection c): this constructor was added in vectors after the release of Java 2. This type of constructor creates a vector that contains the element of another collection c.
NOTE: Once the initial capacity limit is reached, the size of the vector increases after each reallocation cycle. The default increment factor is 100% which means the size of the vector gets doubled.
Insertion in Vectors in Java
Similar to ArrayList, Vectors do have their own built-in legacy methods for inserting elements into vector. Now see the piece of code below for better understanding.
import java.util.Vector; import java.util.ArrayList; public class vectortest { static Vector<Integer> addCollectionMethod(Vector vec) { ArrayList<Integer> a = new ArrayList<Integer>(); a.add(20); a.add(40); a.add(50); vec.addAll(a); //all elements of arraylist are now added to vectors return vec; } public static void main(String[] args) { Vector<Integer> v = new Vector<Integer>(); v.add(10); //arraylist method, added in jdk 1.2 v = addCollectionMethod(v); System.out.println(v); v.addElement(60); //vector's legacy method v.insertElementAt(30, 2); //30 is inserted at index 2 System.out.println(v); } }
OUTPUT:
[10, 20, 40, 50] [10, 20, 30, 40, 50, 60]
- The add(), addAll() methods inserts the element at the last.
- The addElement() is a legacy method that behaves like add() method that inserts an element at last.
- The InsertElementAt() method is a legacy method that inserts the element at the specified position.
Deletion in Vectors in Java
Similar to insertion we have built-in methods for deletion also so. Observe the code below and try to solve it on your own.
import java.util.*; public class VectorTest { static Vector<Integer> addCollectionMethod(Vector vec) { ArrayList<Integer> a = new ArrayList<Integer>(10); a.add(10); a.add(20); a.add(30); a.add(40); a.add(50); a.add(70); a.add(80); a.add(60); vec.addAll(a); //all elements of arraylist are now added to vectors return vec; } public static void main(String[] args) { Vector<Integer> v = new Vector<Integer>(); v = addCollectionMethod(v); System.out.println(v); v.removeElementAt(5); //removes element from index 5 v.removeElement(40); //removes 40 v.remove(1); //removes element from index 1 System.out.println(v); v.removeAllElements(); System.out.println(v); } }
OUTPUT:
[10, 20, 30, 40, 50, 70, 80, 60] [10, 30, 50, 80, 60] []
- removeAllElements(): legacy method which removes all the elements from vector and its size becomes zero.
- removeElementAt(int index): a legacy method that removes the element from the specified index position.
- removeElement(Object element) : legacy method which removes the specified element from vector. If the element specified has appeared more than once then its first instance will be removed.
- remove() : it is not a legacy method, but it behaves same as removeElementAt() method.
Leave a Reply