Introduction to Vector in Java
A vector in java is one of the legacy classes available in java and is available in java. util package. Vector internally contains a dynamical array that can increase or decrease itself as per requirement. Components inside a vector can be accessed using an integer-valued index. In comparison to the array list, the vector is thread-safe and contains legacy methods that are not available in the collection framework. It is to be noted that since the vector is a legacy class, it is immutable and thread-safe in nature.
Syntax:
// create vector instance
Vector<T> vectorobj=new Vector<T>();
//call required method
vectorobj.methodName();
Here is the declaration of vector class in java.
public class Vector<T> extends AbstractList<T>implements List<T>,RandomAccess,Cloneable,Serializable
How to Create Vector in Java?
Vector is a part of the Legacy framework. The following are the main constructors of the Vector Class.
- Vector(): This creates an empty vector with a default capacity of 10 elements.
- Vector(Collection<? extends T > c): This creates a vector containing all specified collection elements in the order the collection’s iterator returns them.
- Vector(int initialCapacity): This creates an empty vector with a specified initial capacity.
- Vector(int initialCapacity, int incrementCapacity): This creates an empty vector with specified initial capacity and capacity increment.
If incremental capacity in the vector is specified along with initial capacity, the vector will expand itself according to incremental capacity in each element addition cycle, but if increment capacity is not specified, then the vector’s capacity gets doubled in each addition cycle.
Examples to Implement Vector in Java
Here are some of the examples of vector in java as follows:
Example #1
Let us see a basic example of the vector class in java as given below.
Code:
import java.util.*;
public class VectorDemo{
public static void main(String args[]){
// Creating a vector instance which is not type safe and can contain any type of element
Vector vector= new Vector();
vector.add(1);
vector.add(5);
vector.add("Edubca");
vector.add("Java");
vector.add("Training");
vector.add(4);
System.out.println("Vector has the following elements " + vector);
// Creating a vector instance which is type safe
// This can contain only String elements. If any other type of element is added, it will result in compile //time error.
Vector<String> typeSafeVector= new Vector<String>();
typeSafeVector.add("Edubca");
typeSafeVector.add("Java");
typeSafeVector.add("Training");
System.out.println("Vector has the following elements " + typeSafeVector);
}
}
Output:
The above code shows the creation of a java vector class, and it adds a method. Also, we have seen how to create a type-safe vector. The following output will be produced:
Example #2
In this example, we will see some more methods of Vector class like add at the specified position and get the element at a specified index in vector.
Code:
import java.util.*;
public class VectorDemo{
public static void main(String args[]){
Vector<String> vector= new Vector<String>();
vector.add("Edubca");
vector.add("Java");
vector.add("Training");
System.out.println("Original Vector has the following elements " + vector);
vector.add(1, "Provides"); // adding element at specified position
System.out.println("Modified Vector has the following elements " + vector);
String elementAtFirst= vector.get(0); //get element at first position in vector
System.out.println("Element at first position in vector is " + elementAtFirst);
String elementAtThird = vector.get(2); //get element at third position in vector
System.out.println("Element at Third position in vector is " + elementAtThird );
}
}
In the above example, we have seen how to create a Vector class and usage of its methods. The above code will display the following as output.
Output:
Example #3
In this example, we will see how to create a vector from an existing collection and contain the vector class method.
Code:
import java.util.*;
public class VectorDemo{
public static void main(String args[]){
HashSet set =new HashSet();
set.add("first");
set.add("second");
set.add("third");
set.add("fourth");
Vector vector= new Vector(set);
System.out.println("Vector created from Hashset is " + vector);
System.out.println(vector.contains("third"));
System.out.println(vector.contains("fifth"));
}
}
Output:
Example #4
In this example we will see the use of remove(), isEmpty() and clear() methods in Vector.
Code:
import java.util.*;
public class VectorDemo{
public static void main(String args[]){
HashSet set =new HashSet();
set.add("This");
set.add("is");
set.add("Vector");
set.add("Class");
set.add("Demo");
set.add("In");
set.add("Java");
Vector vector= new Vector(set);
System.out.println("Vector created from Hashset is " + vector);
vector.remove(1);
vector.remove(3);
System.out.println("Vector after removal of Elements " + vector);
System.out.println("Is Vector Empty : " + vector.isEmpty());
vector.clear(); // Clear All Elements of Vector
System.out.println("Vector After Calling Clear method : " + vector);
System.out.println("Is Vector Empty : " + vector.isEmpty());
}
}
Here is the output produced after running the above code.
Output:
Example #5
In this example, we will see how to iterate elements of a vector using an iterator.
Code:
import java.util.*;
public class VectorDemo{
public static void main(String args[]){
Vector<String> vector= new Vector<String>();
vector.add("Welcome");
vector.add("To");
vector.add("Edubca");
vector.add("Java");
vector.add("Training");
Iterator it=vector.iterator();
System.out.println("Vector elements as traversed by iterator are : ");
while(it.hasNext()){
System.out.println(it.next());
}
}
}
The above code will produce the following output.
Output:
Conclusion
From the above discussion, we have a clear understanding of what is Vector in java, how it’s created, and the different methods available in vector class. Also, the vector is thread-safe, and therefore it can be used in a multithreading environment.
Recommended Articles
This is a guide to Vector in Java. Here we discuss the basic concept, creating a vector in java, and examples and code implementation. You can also go through our suggested articles to learn more –